interface A {
readonly a: string
b: string
}
const aaa: A = {a: 'hello', b: 'world'}
aaa.a = 'good'
readonly 타입을 통해서 실수로 타입을 변경하는 걸 막아줄 수 있다.
type A = {a: string, b: string, c: string, ...}
/**
* 만약에 객체의 많은 속성의 타입이 string일때 위처럼 나열하지 않고,
*/
type A = { [key: string]: string }
/**
* 이렇게 index signature를 이용해서 추론할 수 있다ㅏ.
*/
const aaa: A = {a: 'hello', b: 'world'}
type B = 'Human' | 'Mammal' | 'Animal'
type A = { [key in B]: string }
/**
* key in B를 통해 key의 타입을 더욱 좁게 만들수 있다
*/
const aaa: A = {Human: 'hello', Mammal: 'world', Animal: '123'}
type B = 'Human' | 'Mammal' | 'Animal'
type A = { [key in B]: string }
/**
* key in B를 통해 key의 타입을 더욱 좁게 만들수 있다
*/
const aaa: A = {Human: 'hello', Mammal: 'world'}
변수 aaa에서 타입오류가 발생한다.
오류를 해석해보면 'Animal' property가 type A에서는 필수적인데 빠져있다
의미이다.
type A = { [key in B]: string }
/**
* type A = {
Human: string;
Mammal: string;
Animal: string;
}
*/
type AA = { [key in B]?: string }
/**
* ? 를 추가하면 optional로 변경할 수 있다
* type AA = {
Human?: string | undefined;
Mammal?: string | undefined;
Animal?: string | undefined;
}
*/