나는 항상 --noImplicitAny 플래그로 Typescript를 컴파일합니다. 타입 검사를 최대한 타이트하게하고 싶을 때 이치에 맞습니다.
내 문제는 다음 코드로 오류가 발생한다는 것입니다 Index signature of object type implicitly has an 'any' type.
interface ISomeObject {
firstKey: string;
secondKey: string;
thirdKey: string;
}
let someObject: ISomeObject = {
firstKey: 'firstValue',
secondKey: 'secondValue',
thirdKey: 'thirdValue'
};
let key: string = 'secondKey';
let secondValue: string = someObject[key];
중요한 것은 키 변수가 응용 프로그램의 다른 곳에서 왔으며 객체의 키 중 하나 일 수 있다는 것입니다.
나는 명시 적으로 타입을 캐스팅하려고 시도했다.
let secondValue: string = <string>someObject[key];
아니면 내 시나리오는 불가능 --noImplicitAny합니까?