as const와 generic으로 타입 추론 하기
as const부터 알아보자.
// Works with no types referenced or declared.
// We only needed a single const assertion.
function getShapes() {
let result = [
{ kind: "circle", radius: 100 },
{ kind: "square", sideLength: 50 }
] as const;
return result;
}
for (const shape of getShapes()) {
// Narrows perfectly!
if (shape.kind === "circle") {
// as const로 readonly로 선언될 경우, shape. 했을 때 radius만 나온다.
// 만약 as const를 제거하면 shape. 했을 경우 radius, sideLength가 나온다.
console.log("Circle radius", shape.radius);
} else {
console.log("Square side length", shape.sideLength);
}
}Generics
motivation and sample
ts와 redux 예제 만들다가 궁금했던 점
summary, FAQ
참조
Last updated
Was this helpful?