score:2
Accepted answer
import * as React from "react";
import { render } from "react-dom";
import { useEffect, useState } from "react";
type X = "fname" | "lname" | "age" | "height";
const App: React.FC = props => {
const [state, setState] = useState<{ array1: X[] }>({ array1: [] });
const Translations: {
[key: string]: {
fname: string;
lname: string;
age: string;
height: string;
};
} = {
en: {
fname: "First Name",
lname: "Last Name",
age: "Age",
height: "Height"
},
cn: {
fname: "名字",
lname: "姓",
age: "年龄",
height: "高度"
}
};
const object1 = Translations["en"];
useEffect(() => {
const array1: X[] = ["fname", "lname", "age", "height"];
setState({ ...state, array1 });
}, [state]);
return (
<div className="App">
<ul>
{state.array1.map((item: X, index: number) => (
<li key={index}>{object1[item]}</li>
))}
</ul>
<p>{object1.fname}</p>
</div>
);
};
const rootElement = document.getElementById("root");
render(<App />, rootElement);
https://codesandbox.io/s/empty-breeze-kqpqg
Or to make it cleaner and DRY it up you can do : https://codesandbox.io/s/angry-https-rc66o?fontsize=14
score:0
Adding an index signature will let TypeScript know what the type should be. You can do this to get rid of the error
const Translations: {
[key: string]: {
fname: string;
lname: string;
age: string;
height: string;
[key: string]: string;
};
} = {
en: {
fname: "First Name",
lname: "Last Name",
age: "Age",
height: "Height"
},
cn: {
fname: "名字",
lname: "姓",
age: "年龄",
height: "高度"
}
};
Source: stackoverflow.com
Related Query
- react typescript error - element implicitly has an any type because expression of type string cant be used to index type {}
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript
- Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index
- React Typescript: Element implicitly has an 'any' type because type has no index signature
- TypeScript - Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
- Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- issue typing an object react typescript - Element implicitly has an 'any' type because type'
- Typescript react - Could not find a declaration file for module ''react-materialize'. 'path/to/module-name.js' implicitly has an any type
- TS | Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<SecurityMode, boolean>'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- Typescript-React State: Element implicitly has an 'any' type because type 'State' has no index signature
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'
- Graphql React Typescript error binding element 'currency' implicitly has an 'any' type
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{...}'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Breakpoints'
- how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?
- Global window object: Element implicitly has an 'any' type because index expression is not of type 'number'.ts
- React typescript error: Element implicitly has an 'any' type
- element implicitly has an 'any' type because type 'yyy' has no index signature.ts(7017)
- Typescript when I map imports it says 'Element implicitly has an 'any' type because expression of type 'string' to index type 'typeof import'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Item'
- Nested Object -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- TypeScript: Element implicitly has an 'any' type because type typeof css file has no index signature
- Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type
- error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index 'ModuleType'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Number'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'
More Query from same tag
- chrome devtools debug react, hit "refresh" cause debug stuck
- Positioning a series labels across the screen
- Plotting a function in React with dynamic input
- React Datepicker - wrong date showing when I bind to enable e.preventDefault()
- Redux action creator finishes, update subscribed component
- How to access 'store' in react redux? (or how to chain actions)
- How to structure a react project correctly?
- Transition not working with React useState hook and styled-component
- Importing third party scripts into React
- React Context, how to update context in one branch, to use in another branch
- Manifest.json for progressive web app not working
- The value is not saved when submit form
- Redux-form redirect to unknown url
- How do I iterate over a JSON file in react?
- Pass map's argument to function in ReactJS
- CSS : How to put a div on top of the modal-backdrop
- How to update state from other component?
- Running python scripts in Next.js
- How to change height or width of pdf in react (npm react-pdf)
- React JS - using back button to get previous searches
- Buttons are displayed before the api loads inside the table and loader not working
- How to clear TextField after ENTER key press in React
- CSS - How do I create a grid that takes up the remaining width of screen (without counting sidebar)
- useState updating for all list elements
- Updating nested data in redux store
- How should I use "redux-thunk" for Async Initial state ? (react/redux)
- MalformedSecurityHeader error in signed url - Header was included in signedheaders, but not in the request
- Next-Auth Signout within jwt callback
- Comparing two array of objects and getting the difference
- Canvas drawing not loading in puppeteer