https://github.com/LearningTypeScript/projects/tree/main/projects/objects/the-typer
GitHub - LearningTypeScript/projects: Hands-on real world projects that will help you exercise your knowledge of TypeScript.
Hands-on real world projects that will help you exercise your knowledge of TypeScript. - GitHub - LearningTypeScript/projects: Hands-on real world projects that will help you exercise your knowledg...
github.com
당신의 오랜 친구가 최근 당신에게 희귀한 보물을 포함하고 있다는 지도를 주었습니다.
지도에 TypeScript 타입 어노테이션이 없지만 허용되는 것처럼 보입니다.
지도를 더 유용하게 사용할 수 있도록 타입 어노테이션을 작성할 수 있습니까?
## 명세
"/index.ts"에서 TypeScript 타입 어노테이션을 추가하여 TypeScript가 오류 없이 컴파일되도록 합니다.
## 노트
- any를 사용하거나 암묵적인 any를 남기지 마십시오.
- _Learning TypeScript_에서 다루지 않은 배열이나 함수는 사용하지 마십시오.
- 런타임 코드를 변경하지 마십시오. 그대로 사용해도 됩니다.
type CurrentBase = {
name: string;
proximity: number;
treasure?: string;
};
type Stream = BeginStream | MiddleStream | EndStream;
type StreamBase = CurrentBase & {
type: "stream";
};
type BeginStream = StreamBase & {
area: "begin";
downstream: Current;
};
type MiddleStream = StreamBase & {
area: "middle";
downstream: Current;
upstream: Current;
};
type EndStream = StreamBase & {
area: "end";
upstream: Current;
};
type Clearing = CurrentBase & {
type: "clearing";
through?: Current;
};
type Path = CurrentBase & {
type: "path";
shortcut?: Current;
through: Current;
};
type Town = CurrentBase & {
type: "town";
around?: Current;
through?: Current;
};
type Current = Clearing | Path | Town | Stream;
let current: Current | undefined = {
name: "Woesong Bridge",
proximity: 100,
through: {
area: "middle",
downstream: {
around: {
area: "end",
upstream: {
name: "Vizima",
proximity: 30,
type: "clearing",
},
name: "White Orchard Creek",
proximity: 25,
type: "stream",
},
name: "Oxenfurt Gate",
proximity: 40,
through: {
name: "Vergen Tunnel",
proximity: 20,
shortcut: {
proximity: 30,
name: "Crow's Perch",
type: "town",
},
through: {
area: "begin",
downstream: {
through: {
treasure: "rare playing cards",
name: "Reuven's Treasure",
proximity: 0,
type: "clearing",
},
name: "Gate of the Hierarch",
proximity: 10,
type: "town",
},
name: "Founders Stream",
proximity: 25,
type: "stream",
},
type: "path",
},
type: "town",
},
name: "Yavina River",
proximity: 50,
type: "stream",
upstream: {
name: "Merchants' Trail",
proximity: 65,
through: {
name: "Beauclair",
proximity: 70,
type: "town",
},
type: "path",
},
},
type: "path",
};
let treasure;
while (current) {
console.log(`At: ${current.name}`);
switch (current.type) {
case "clearing":
current = current.through;
break;
case "path":
current =
current.shortcut &&
current.shortcut.proximity < current.through.proximity
? current.shortcut
: current.through;
break;
case "town":
if (!current.around) {
current = current.through;
} else if (!current.through) {
current = current.around;
} else {
current =
current.around.proximity < current.through.proximity
? current.around
: current.through;
}
break;
case "stream":
switch (current.area) {
case "begin":
current = current.downstream;
break;
case "end":
current = current.upstream;
break;
case "middle":
current =
current.downstream.proximity < current.upstream.proximity
? current.downstream
: current.upstream;
break;
}
}
if (!current) {
console.log("Hmm. Dead end.");
} else if (current.treasure) {
treasure = current.treasure;
break;
}
}
if (treasure) {
console.log(`This will do nicely: ${treasure}.`);
} else {
console.log("Nothing going.");
}
중첩된 객체의 타입을 작성하는 문제.
조건문을 근거로 판별된 유니언을 작성해야 한다.
728x90