728x90
Index Signatures
[key: string]: string처럼 key값의 이름 대신 key값의 type을 대괄호 안에 명시해주는 형식이다.
타입 프로퍼티의 이름은 모르지만 타입은 알고 있을 때 인덱스 시그니처를 통해 인터페이스를 정의할 수 있다.
declare interface CodeMap {
[key: string]: CodeItem[];
}
또는 하나의 인터페이스에 동일한 타입의 key-value가 반복될 때 인덱스 시그니처를 통해 코드를 간략하게 유지할 수 있다.
interface Student {
studentId: string;
name: string;
className: string;
subject: string;
};
interface Student {
[key: string]: string;
};
[ 참고 자료 ]
타입스크립트 인터페이스를 임의의 객체에 할당하지 못하는 문제
인터페이스로 정의한 객체가 있다. 임의의 객체를 다루는 함수가 있다. 나는 이 둘을 함께 쓰고 싶다.
velog.io
[TypeScript]인덱스 시그니처(Index Signature) 사용 방법
인덱스 시그니처(Index Signature)이란? 인덱스 시그니처(Index Signature)는 { [Key: T]: U } 형식으로 객체가 여러 Key를 가질 수 있으며, Key와 매핑되는 Value를 가지는 경우 사용합니다. 이번 포스팅에서는
developer-talk.tistory.com
728x90
'Typescript' 카테고리의 다른 글
어떤 상황에서 ==와 ===를 써야 할 것인가? (2) | 2024.11.20 |
---|---|
Typescript 제네릭(generic)이란? (0) | 2024.09.26 |
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type (0) | 2024.03.27 |
Type of emit value is always 'any' with new style defineEmits (0) | 2024.03.27 |
Typescript 인터페이스 네이밍 규칙 어떻게 할까? | 접두사 I를 사용하면 안 되는 이유 (1) | 2024.03.27 |