TypeScript ์์๋ ๊ธฐ๋ณธํ ์๋ฃํ, ์ฐธ์กฐํ ์๋ฃํ์ธ์๋ ์ถ๊ฐ๋ก ํ์ ์ ์ ๊ณตํ๋ค.
1. tuple
๊ธธ์ด์ ๊ฐ ์์์ ํ์ ์ด ์ ํด์ง ๋ฐฐ์ด์ ์ ์ฅํ๋ ํ์
let arr:[string, number] = ["Hi",6];
arr[1].concat("!");
//Error, 'number'does not have'concat’
//์ ์ํ์ง์์ index ํธ์ถ ์ ์ค๋ฅ
arr[3]="hello";
//Error, Property '3'does not exist on type'[string,number]
2. enum
- ํน์ ๊ฐ๋ค์ ์งํฉ์ ์ ์ฅํ๋ ํ์
- enumerable, ์ฆ ์ด๊ฑฐ ๊ฐ๋ฅํ ๋ฐ์ดํฐ ํ์ ์ ๋งํ๋ค.
- ๋ฐ์ดํฐ์ ์์๊ฐ ์๋๋ฐ, ๊ทธ ์์์ ๋ง๊ฒ ๊ฐ์ ์ฃผ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค.
- ์์๋ ์์๋ก ๋งค๊ธฐ๋ฉฐ, 1์ฉ ์ฆ๊ฐํ๋ ๊ผด์ด๋ค.
ํน์ ๊ฐ(์์)๋ค์ ์งํฉ์ ์ ์ฅํ๋ ํ์ , ์ฐ๊ด๋ ์์ดํ ๋ค์ ํจ๊ป ๋ฌถ์ด์ ํํํ ์ ์๋ ์๋จ์ด๋ค.
enum CardinalDirection {
North = 1,
East, // 2 1์ฉ ์ถ๊ฐ๋๋ค.
South, // 3
West,
};
const UserDirection: CardinalDirection = {
North: CardinalDirection.North,
East: CardinalDirection.East,
South: CardinalDirection.South,
West: CardinalDirection.West,
};
3. any
๋ชจ๋ ํ์
์ ์ ์ฅํ ์ ์๋ค. ์ปดํ์ผ ์ค ํ์
๊ฒ์ฌ๋ฅผ ํ์ง ์๋๋ค.
ํ์ง๋ง any๋ฅผ ๋จ๋ฐํ๋ฉด Typescirpt๋ฅผ ์ฐ๋ ์ด์ ๊ฐ ์๊ธฐ ๋๋ฌธ์, ์ ํ์ ์ผ๋ก ์ฌ์ฉํ๋ค.
let str: any = "hi";
let num: any = 10;
let arr: any = ["a", 2, true];
4. void
๋ณดํต ํจ์์์ ๋ฐํ ๊ฐ์ด ์์ ๋, any์ ๋ฐ๋ ํ์
// ๋ณ์์๋ undefined์ null๋ง ํ ๋นํ๊ณ , ํจ์์๋ ๋ฐํ ๊ฐ์ ์ค์ ํ ์ ์๋ ํ์
let unknown: void = undefined;
function sayHi(): void {
console.log("hi");
}
5. never
๋ฐ์ํ ์ ์๋ ํ์
// ํญ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๊ฑฐ๋ ์ ๋ ๋ฐํํ์ง ์๋ ๋ฐํ ํ์
// ์ข
๋ฃ๋์ง ์๋ ํจ์
function neverEnd(): never {
while (true) {}
}
// Error: A function returning 'never' cannot
have a reachable end point.ts(2534)
function neverEnd(): never {
while (true) {
break;
}
}
// ํญ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ํจ์
function error(message: string): never {
throw Error(message)
}
๋ฐ์ํ
'๐ Front > ๐ซ Typescript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React+Typescript] React.FC๋ฅผ ๊ตณ์ด ์ฌ์ฉํ์ง ์์๋ ๋๋ ์ด์ (0) | 2021.09.06 |
---|---|
[Typescript]: type, interface, generics ์ฌ์ฉํ๊ธฐ (0) | 2021.09.06 |
Typescript ์์ฑ์, public, private (0) | 2021.09.06 |
Typescript: ์ค๊ธ(Type Aliases/Type Guard) (0) | 2021.09.06 |
[Typescript] ํ์ ์คํฌ๋ฆฝํธ ์ ์จ์? (0) | 2021.07.13 |