일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
- cloud hsm 사용하기
- 체인의정석
- Vue.js
- SBT표준
- vue기초
- ethers typescript
- ethers websocket
- 러스트 기초 학습
- ethers type
- ethers v6
- 스마트 컨트렉트 함수이름 중복
- 컨트렉트 동일한 함수이름 호출
- cloud hsm 서명
- redux toolkit 설명
- 오블완
- git rebase
- 스마트컨트렉트 함수이름 중복 호출
- redux 기초
- cloud hsm
- 스마트컨트렉트 예약어 함수이름 중복
- Vue
- 티스토리챌린지
- 러스트 기초
- rust 기초
- erc4337
- ambiguous function description
- 계정추상화
- erc4337 contract
- 러스트기초
- 머신러닝기초
- Today
- Total
체인의정석
typescript에서 enum에 string을 인덱스로 썼을 때 발생하는 문제 해결 본문
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
// bad
const _getKeyValue = (key: string) => (obj: object) => obj[key];
// better
const _getKeyValue_ = (key: string) => (obj: Record<string, any>) => obj[key];
// best
const getKeyValue = <T extends object, U extends keyof T>(key: U) => (obj: T) =>
obj[key];
Bad - the reason for the error is the object type is just an empty object by default. Therefore it isn't possible to use a string type to index {}.
Better - the reason the error disappears is because now we are telling the compiler the obj argument will be a collection of string/value (string/any) pairs. However, we are using the any type, so we can do better.
Best - T extends empty object. U extends the keys of T. Therefore U will always exist on T, therefore it can be used as a look up value.
https://www.angularjswiki.com/angular/how-to-convert-a-string-to-enum-in-typescript-or-angular/
How To Convert a string to Enum in Typescript/Angular | Angular Wiki
To convert string to Enum in Typescript or angular follow the below steps. 1.Pass the given string to Enum object as a key. 2.If the string is part of the enum name entry the value will be returned. 3.And then cast it to the enum object to get enum type of
www.angularjswiki.com
다음 글을 참고하여 해결하였다.
원래 코드
// Change string to LegalPersonNameTypeCode
export function toLegalPersonNameTypeCode(legalPersonNameIdentifierType: string) {
if (LegalPersonNameTypeCode[legalPersonNameIdentifierType]) {
return LegalPersonNameTypeCode[legalPersonNameIdentifierType];
}
throw new Error(`${legalPersonNameIdentifierType} must be one of 'LEGL','SHRT', 'TRAD' `);
}
수정한 코드
// Change string to LegalPersonNameTypeCode
export function toLegalPersonNameTypeCode(legalPersonNameIdentifierTypeParam: string) {
const legalPersonNameIdentifierType: LegalPersonNameTypeCode = (<any>legalPersonNameIdentifierTypeParam)[
legalPersonNameIdentifierTypeParam
];
if (legalPersonNameIdentifierType) {
return legalPersonNameIdentifierType;
}
throw new Error(`${legalPersonNameIdentifierType} must be one of 'LEGL','SHRT', 'TRAD' `);
}
타입을 단순하게 string으로만 지정해 주는 것이 아니라 더 자세히 지정해 주어야 오류가 나지 않는다.
'개발' 카테고리의 다른 글
git cli 주요 명령어 정리 (0) | 2021.07.26 |
---|---|
typescript 커스텀 모듈 만들 시 발생가능한 에러사항 (0) | 2021.07.22 |
package.json의 dependency 살펴보기 (0) | 2021.07.22 |
jest.config - mouduleNameMapper @ 경로 인식 에러 해결하기 (0) | 2021.07.22 |
ts config.json (0) | 2021.07.22 |