React-Native
[RN]Element implicitly has an 'any' type because expression of type 'string' can't be used to index
gimgongta
2022. 10. 25. 10:46
반응형
solusion 1.
const someObj:ObjectType = data;
const field = 'username';
// This gives an error
const temp = someObj[field];
// Solution 1: When the type of the object is known
const temp = someObj[field as keyof ObjectType]
// Solution 2: When the type of the object is not known
const temp = someObj[field as keyof typeof someObj]
Element implicitly has an 'any' type because expression of type 'string' can't be used to index
Trying out TypeScript for a React project and I'm stuck on this error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ train_1: boolean; tra...
stackoverflow.com
solution 2.
type exampleType = {
[index: string]: string,
address1: string,
address2: string,
}
const ExampleData: exampleType = {
address1: 'SampleData1',
address2: 'SampleData2',
}
console.log(ExampleData[address1]) // SampleData1
반응형