let str = "hello";
let n: typeof str; // let n : string
error
function f() {
return { x: 10, y: 3 };
}
type P = ReturnType<f>;
// 'f' refers to a value, but is being used as a type here.
// Did you mean 'typeof f'?
// <>안에는 타입이 들어가야 하기 때문이다.
정상동작
// 해당 문법은 정상동작한다.
function f() {
return { x: 10, y: 3 };
}
type P = ReturnType<typeof f>;
type P = {
x: number;
y: number;
}
type Test = 'A' | 'B' | 'C';
type MappedTest = { [K in Test]: number };
// 위는 아래와 같다.
type MappedTest = {'A' : number} & {'B' : number} & {'C' : number};
// no index signature
type Point = { x: number; y: number };
type P = keyof Point; // type P = 'x' | 'y'
// with index signature
type Arrayish = { [n: number]: unknown };
type A = keyof Arrayish; // type A = number
// 자바스크립트 오브젝트 키는 스트링 타입으로 반드시 강제변환되기 때문에 숫자도 허용한다.
type Mapish = { [k: string]: boolean };
type M = keyof Mapish; // type M = string | number