728x90
์คํ๋ ๋ ์ฐ์ฐ์์ ํจ๊ป ์ฐ๋ ๊ฒฝ์ฐ
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<number> = [1, 2, 3, 4, 5];
const cloths: [string, number] = ['์ฒญ๋ฐ์ง', 2];
console.log(cloths[0]); //์ฒญ๋ฐ์ง
ํํ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ์ง ์๋๋ค.
const cloths: any[] = ["์ฒญ๋ฐ์ง", 2, false];
console.log(cloths[0]); //์ฒญ๋ฐ์ง
console.log(cloths[1]); //2
console.log(cloths[2]); //false
const cloths: (number | boolean | string)[] = ["์ฒญ๋ฐ์ง", 2, false];
console.log(cloths[0]); //์ฒญ๋ฐ์ง
console.log(cloths[1]); //2
console.log(cloths[2]); //false
์ธ๋ฑ์ค ์ ๊ทผ ๋ฐฉ์์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค.
ํํ๋ณด๋ค๋ interface๋, type alias, class ๋ก ๋์ฒดํด์ ์ฌ์ฉํ ๊ฒ!
728x90
'TypeScript > ํ์ ์คํฌ๋ฆฝํธ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ ํ์ ! Discriminated Union (0) | 2022.04.15 |
---|---|
ํ์ ์คํฌ๋ฆฝํธ 1์ฅ ์ฐ์ต๋ฌธ์ | ํ์ ์คํฌ๋ฆฝํธ์ ์ถ๋ก (0) | 2022.04.15 |
์ง์ ํ ํ์ ์คํฌ๋ฆฝํธ์ ์์, Union ํ์ (0) | 2022.04.15 |
ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฐ๋ ์ด์ , type alias์ String Literal Types (0) | 2022.04.15 |
ํ์ ์คํฌ๋ฆฝํธ ๋ง๋ณด๊ธฐ (0) | 2022.03.28 |