상세 컨텐츠

본문 제목

[RN]Element implicitly has an 'any' type because expression of type 'string' can't be used to index

React-Native

by 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]

출처 : https://stackoverflow.com/questions/57086672/element-implicitly-has-an-any-type-because-expression-of-type-string-cant-b

 

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
반응형

관련글 더보기

댓글 영역