score:1
Accepted answer
First of all, there isn't a nice way to do this and it's still in discussion: https://github.com/Microsoft/TypeScript/issues/3500
Below are 2 potential ways to solve your issue.
- Declare the key outside of the loop:
const formElementsArray: { id: string; config: Config }[] = [];
let key: keyof typeof this.state.controls;
for (key in this.state.controls) {
formElementsArray.push({
id: key,
config: this.state.controls[key],
});
}
- Use Object.keys & cast the key to the desired type:
const formElementsArray: { id: string; config: Config }[] = (Object.keys(
this.state.controls
) as (keyof typeof this.state.controls)[]).map((key) => ({
id: key,
config: this.state.controls[key],
}));
You could also make it clearer by using an enum for the control key
enum ControlKey {
email = "email",
password = "password",
}
interface SignInFormState {
controls: {
[key in ControlKey]: Config;
};
isSignUp: boolean;
[key: string]: boolean | Object | Config;
}
then
1b.
const formElementsArray: { id: string; config: Config }[] = [];
let key: ControlKey;
for (key in this.state.controls) {
formElementsArray.push({
id: key,
config: this.state.controls[key],
});
}
or
2b.
const formElementsArray: { id: string; config: Config }[] = (Object.keys(
this.state.controls
) as ControlKey[]).map((key) => ({
id: key,
config: this.state.controls[key],
}));
Hope this helps
Source: stackoverflow.com
Related Query
- Nested Object -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- react typescript error - element implicitly has an any type because expression of type string cant be used to index 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
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript
- 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
- TypeScript - Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'
- Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Item'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
- 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 '{}'
- Element implicitly has an 'any' type because expression of type 'number' 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 '{ 1: Element; 2: Element; 3: Element; }'. TS7053
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type with createStyles
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors'
- 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'
- error TS2602: JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist
- 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
- How do I fix the error "Element implicitly has an 'any' type because the expression of type
- React Typescript: Element implicitly has an 'any' type because type has no index signature
More Query from same tag
- Keeping ref to children created by function as child prop
- How to set callback function as prop for react-paginate and TypeScript?
- how to Group array key value
- How does one programmatically 'open' a Material-UI Select field?
- Disable Unnecessary escape character: \/ no-useless-escape
- Using React : Passing data from SearchBar component to Widget1 component
- edit static files (index.html) from yarn build
- Unable to toggle class on onClick in React
- Add default value option in select tag
- Nesting Emotion 10 classes for cascade
- How can I access existing dom elements when using react render?
- How to identify which menu item has been clicked in React?
- React search (filter) function, add parameter to search
- How to implement react-widgets dropdownlist in reactjs
- React HOC ESLint Error: props spreading is forbidden
- Next JS Image Optimization
- How store binary tree nodes in firestore
- How do I add a query param to Router.push in NextJS?
- How to resolve component with two levels of nested routes?
- Register form similar to facebook once CSS
- How to upload image to aws s3 using react and django rest framework
- How to pass variable to Component received in props?
- Is there a way I could href to a local const file in a link tag
- How to get results in the right way with Graphql
- If all fields on Form successfully validate, page crashes
- How can I UpdateUserProfile when signing up user, in Firebase-v9, with Typescript?
- Serving multiple react apps with client-side routing in Express
- How can I use show component inside a ListItemText?
- What's wrong with this ReactRouter.match() implementation?
- Is it right concept if I used Async Await with react life cycles specially with ComponentDidMount for api calls?