| νμ μ€ν¬λ¦½νΈ νμ λ³μΉ
λκ°μ νμ μ ν λ² μ΄μ μ¬μ¬μ©νκ±°λ, λ λ€λ₯Έ μ΄λ¦μΌλ‘ λΆλ₯΄κ³ μΆμ κ²½μ°μ μ¬μ©!
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"); // μ΄λ΄ κ²½μ° μλ¬λ¨.
// λλ²μ§Έ μΈμμλ μΈκ°μ§μ λ¬Έμμ΄ μ€ νλλ§ μ
λ ₯ν μ μκΈ° λλ¬Έμ!!