TypeScript
์ง์ ํ ํ์ ์คํฌ๋ฆฝํธ์ ์์, Union ํ์
์ ๋์จ ํ์ ์ ํ์ ์คํฌ๋ฆฝํธ์์ ์ฝ๋ฉํ ๋ ๊ต์ฅํ ๋ง์ด ์ฐ๊ฒ ๋ ๊ฒ์ด๋ค. OR ๋๋ ํฉ์งํฉ์ผ๋ก ์ดํดํ๋ฉด ์ถฉ๋ถํ ๊ฒ์ด๋ค. type Direction = "left" | "right" | "up" | "down"; function move(direction: Direction) { console.log(direction); } type TileSize = 8 | 16 | 32; const tileA: TileSize = 16; //const tileB: TileSize = 10; //10 ํ์์ TileSize ํ์์ ํ ๋นํ ์ ์์ต๋๋ค. //function : login -> success, fail type SuccessState = { response: { body: string; }; }; type F..
ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฐ๋ ์ด์ , type alias์ String Literal Types
type alias(๋ด๊ฐ ์๋ก์ด ํ์ ์ ์ ์ํ ์ ์๋ค)๋ฅผ ์ด์ฉํ๋ฉด ๊ธฐ๋ณธ์ ์ธ ํ์ ๋ถํฐ ๋ณต์กํ๊ณ ์ ๊ตํ ํ์ ๊น์ง ์ ์ํ ์ ์๋ค. ๊ฐ๋ น type Text = string; ์์ ๊ฐ์ด ์์ฑํ๋ฉด Text๋ผ๋ ์๋ก์ด ํ์ ์ ๋ฌธ์์ด์ ๋งํ๋ค๊ณ ์ง์ ํด์ฃผ๋ ๊ฒ์ด๋ค. type Text = string; const name: string = "ํ๊ธธ๋"; const nickName: Text = "ํฉ์ง์ด"; console.log(name); //ํ๊ธธ๋ console.log(nickName); //ํฉ์ง์ด type alias ๋ฅผ ํตํด์ ๋ค์๊ณผ ๊ฐ์ด ํน์ ํ๋กํผํฐ๊ฐ ๊ฐ์ ๋ ๊ฐ์ฒด ํ์ ์ ๋ง๋ค ์๋ ์๋ค. ๊ทธ๋ฆฌ๊ณ ๋ณ์์ ํ์ ์ type alias๋ก ์์ฑํ ๊ฒ์ผ๋ก ์ง์ ํด์ฃผ๋ฉด ํน์ ํ๋กํผํฐ๋ง ํ ๋นํ ์ ์์ ๋ฟ๋๋ฌ ํน์ ํ๋กํผํฐ์ ํ..
์คํ๋ ๋ ์ฐ์ฐ์, ๋ฐฐ์ด, ํํ
์คํ๋ ๋ ์ฐ์ฐ์์ ํจ๊ป ์ฐ๋ ๊ฒฝ์ฐ function addNum(...numbers: number[]): number { return numbers.reduce((a, b) => a + b); } console.log(addNum(1, 2)); console.log(addNum(1, 2, 3)); console.log(addNum(1, 2, 3, 4)); 3 6 10 ๋ฐฐ์ด๊ณผ ํํ nst fruits: string[] = ["์ฌ๊ณผ", "๋ฐ๋๋"]; const numbers: Array = [1, 2, 3, 4, 5]; const cloths: [string, number] = ['์ฒญ๋ฐ์ง', 2]; console.log(cloths[0]); //์ฒญ๋ฐ์ง ํํ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋๋ค. const cloth..
ํ์ ์คํฌ๋ฆฝํธ ๋ง๋ณด๊ธฐ
https://youtu.be/e_lU39U-5bQ *์๋ฆฌ๋ ์์ ์บก์ฒ์ ๋๋ค. function ํจ์๋ช (๋งค๊ฐ์ธ์ : ํ) : ๋ฐํํ{ //์๋ฌด์ฝ๋ return ๋ฐํํ์ ๋ง๋ ์๋ฃํ; } }