[RN]Element implicitly has an 'any' type because expression of type 'string' can't be used to index
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
[React-Native]터미널을 ARM64 아키텍처로 실행 (0) | 2024.07.22 |
---|---|
[React Native]특정 ios 에뮬레이터 실행 (0) | 2024.01.02 |
[React-Native]Not Found - GET https://registry.npmjs.org/react-native-template-react-native-template-typescript 에러 해결법 (0) | 2022.06.27 |
[React-Native]Typescript 프로젝트 만들기 (0) | 2022.06.27 |
[React-Native]리액트 네이티브 Fabric (0) | 2022.05.28 |
댓글 영역