| νƒ€μž… 슀크립트 νƒ€μž… 별칭

λ˜‘κ°™μ€ νƒ€μž…μ„ ν•œ 번 이상 μž¬μ‚¬μš©ν•˜κ±°λ‚˜, 또 λ‹€λ₯Έ μ΄λ¦„μœΌλ‘œ λΆ€λ₯΄κ³  싢은 κ²½μš°μ— μ‚¬μš©!

Type Aliases: type ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•΄μ„œ μƒˆλ‘œμš΄ νƒ€μž…μ„ μ •μ˜ν•  수 μžˆλ‹€

βœ”οΈ Type Alias 예제1

type Text = string;
const name: Text = "april"; // const name: string = 'april';
const address: Text = "korea";

type Num = number;
const age: Num = 30; // const age: number = 30;

type Student = {
  name: string;
  age: number;
};
const student: Student = {
  name: "april",
  age: 26,
};


βœ”οΈ Type Alias 예제2

type Point = {
  x: number;
  y: number;
};

function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}

printCoord({ x: 100, y: 100 });



| Literal Types

βœ”οΈ Literal Types 예제1

type Name = "name";
let aprilName: Name;
aprilName = "name";


βœ”οΈ Literal Types 예제2

function printText(s: string, alignment: "left" | "right" | "center") {
  // ...
}
printText("Hello world", "left");
printText("G'day mate", "centre"); // 이럴 경우 μ—λŸ¬λ‚¨.
// λ‘λ²ˆμ§Έ μΈμžμ—λŠ” μ„Έκ°€μ§€μ˜ λ¬Έμžμ—΄ 쀑 ν•˜λ‚˜λ§Œ μž…λ ₯ν•  수 있기 λ•Œλ¬Έμ—!!