keyof
The keyof operator takes an object type and produces a string or numeric literal union of its keys.
type P is the same type as “x” | “y”
If the type has a string or number index signature, keyof will return those types instead.
M is
string | number
— this is because JavaScript object keys are always coerced to a string.※ 인덱스 시그니처(Index Signature)는 { [Key: T]: U } 형식으로
객체가 여러 Key를 가질 수 있으며, Key와 매핑되는 Value를 가지는 경우 사용한다.
typeof
TypeScript adds a
typeof
operator you can use in a type context
to ref to the type of a variable or property.
combined with other type operators, you can use typeof to conveniently express many patterns.
Remember that values and types aren’t the same thing. To refer to the type that the value f has,
we use typeof.
기본적으로 값은 “값 공간”에 존재한다.
값 공간에 있는 값에 typeof 연산자를 적용하여 값 공간의 값을 “타입 공간”에서 타입으로
이용할 수 있다.
응용하기
ValueOf
이는 다음과 같이 사용할 수 있다.
ValueOf를 사용하지 않는다면 아래와 같이도 사용할 수 있다.