type alias(๋ด๊ฐ ์๋ก์ด ํ์ ์ ์ ์ํ ์ ์๋ค)๋ฅผ ์ด์ฉํ๋ฉด ๊ธฐ๋ณธ์ ์ธ ํ์ ๋ถํฐ ๋ณต์กํ๊ณ ์ ๊ตํ ํ์ ๊น์ง ์ ์ํ ์ ์๋ค.
๊ฐ๋ น
type Text = string;
const name: string = "ํ๊ธธ๋";
const nickName: Text = "ํฉ์ง์ด";
console.log(name); //ํ๊ธธ๋
console.log(nickName); //ํฉ์ง์ด
type alias ๋ฅผ ํตํด์ ๋ค์๊ณผ ๊ฐ์ด ํน์ ํ๋กํผํฐ๊ฐ ๊ฐ์ ๋ ๊ฐ์ฒด ํ์ ์ ๋ง๋ค ์๋ ์๋ค.
๊ทธ๋ฆฌ๊ณ ๋ณ์์ ํ์ ์ type alias๋ก ์์ฑํ ๊ฒ์ผ๋ก ์ง์ ํด์ฃผ๋ฉด
ํน์ ํ๋กํผํฐ๋ง ํ ๋นํ ์ ์์ ๋ฟ๋๋ฌ
ํน์ ํ๋กํผํฐ์ ํ์ ๋ ๋ง๊ฒ ๋ฃ์ด์ฃผ์ด์ผ ํ๋ค.
type Address = {
๋์๋ช
: string;
๋๋ก๋ช
: string;
๊ฑด๋ฌผ๋ฒํธ: number;
};
const myAddress: Address = {
๋์๋ช
: "์์ธํน๋ณ์",
๋๋ก๋ช
: "๊ฐ๋ณ๋๋ก29",
๊ฑด๋ฌผ๋ฒํธ: 239,
};
console.log(myAddress); //{ '๋์๋ช
': '์์ธํน๋ณ์', '๋๋ก๋ช
': '๊ฐ๋ณ๋๋ก29', '๊ฑด๋ฌผ๋ฒํธ': 239 }โ
์ด๋ฐ ์์ผ๋ก ์๋ก์ด ํ์ ์ ์ฐ๊ณ ์ถ๋ค๋ฉด type alias๋ฅผ ์ด์ฉํ ์ ์๋ค.
์์ํ์ ๋ฟ๋ง ์๋๋ผ ์ค๋ธ์ ํธ ํํ๋ ์ ์ํ ์ ์๋ค.
์ด์ ์ฐ๊ฒฐ๋ ๋ ๋ค๋ฅธ ๋ฉ์ง ํ์ ์
String Literal Types ์ด๋ค.
ํ์ ์ ๋ฌธ์์ด๋ก๋ ์ง์ ํ ์ ์๋ค. ์ด๊ฒ ๋ฌด์จ ๋ง์ด๋๋ฉด,
[์์1]
type Name = "name";
let friendName : Name;
// friendName = '์งฑ๊ตฌ'; ์๋ฌ
friendName = "name"; //์คํธ๋ง๋ฆฌํฐ๋ดํ์
์ ๊ฐ์ ์คํธ๋ง๋ง ๋ฃ์ด์ค ์ ์๋ค.
[์์2]
type Json = "Json";
//let json :Json = '์ ์ด์จ'; //์ค๋ฅ
let json = "Json";
[์์]
type isTrue = true;
let build1: isTrue = true;
//let build2: isTrue = false; //์๋ฌ
'TypeScript > ํ์ ์คํฌ๋ฆฝํธ TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ ํ์ ! Discriminated Union (0) | 2022.04.15 |
---|---|
ํ์ ์คํฌ๋ฆฝํธ 1์ฅ ์ฐ์ต๋ฌธ์ | ํ์ ์คํฌ๋ฆฝํธ์ ์ถ๋ก (0) | 2022.04.15 |
์ง์ ํ ํ์ ์คํฌ๋ฆฝํธ์ ์์, Union ํ์ (0) | 2022.04.15 |
์คํ๋ ๋ ์ฐ์ฐ์, ๋ฐฐ์ด, ํํ (0) | 2022.04.15 |
ํ์ ์คํฌ๋ฆฝํธ ๋ง๋ณด๊ธฐ (0) | 2022.03.28 |