728x90
type direction = "up" | "down" | "left" | "right";
let position = { x: 0, y: 0 };
function move(dir: direction) {
if (dir === "up") {
position.y++;
}
if (dir === "down") {
position.y--;
}
if (dir === "left") {
position.x--;
}
if (dir === "right") {
position.x++;
}
}
console.log(position); // { x: 0, y: 0}
move("up");
console.log(position); // { x: 0, y: 1}
move("down");
console.log(position); // { x: 0, y: 0}
move("left");
console.log(position); // { x: -1, y: 0}
move("right");
console.log(position); // { x: 0, y: 0}
728x90
'TypeScript > ํ์ ์คํฌ๋ฆฝํธ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ ์คํฌ๋ฆฝํธ ์ ์ฉ ์์] ๊ฐ์ฒด์งํฅ 4๊ฐ์ง ์์น (0) | 2022.04.17 |
---|---|
ํ์ ์คํฌ๋ฆฝํธ ๋ก๋ฉ ์ํ ํ์ ์์ (0) | 2022.04.17 |
ํ์ ์คํฌ๋ฆฝํธ ๊ณ์ฐ๊ธฐ ํจ์ ๋ง๋ค๊ธฐ (0) | 2022.04.17 |
Union๊ณผ ๋ ๋ค๋ฅธ Intersection ํ์ (0) | 2022.04.15 |
ํ์ ํ์ ! Discriminated Union (0) | 2022.04.15 |