Object
λ³μλͺ
: { key1: νμ
, key2: νμ
, ... }
λ³μ μ μΈλ¬Έμ νμ₯ν΄ μμ κ°μ ννλ‘ νμ
μ λͺ
μν μ μμ΅λλ€.
νμ
μ£Όμ(type annotation)μ΄λΌκ³ ν©λλ€.
μμ
const player: { name: string; age: number } = {
name: "kim",
};
Optional
λ³μ/νλ‘νΌν°λͺ
?: νμ
λ¬Όμνλ₯Ό λΆμ¬μ νΉμ νλ‘νΌν°λ ν¨μμ λ§€κ°λ³μλ₯Ό μ΅μ
λλ‘ λ§λ€ μ μμ΅λλ€.
μμ
// μΈν°νμ΄μ€ νλ‘νΌν°μ μ μ©
interface PaintOptions {
shape: Shape;
xPos?: number;
yPos?: number;
}
// κ°μ²΄ νλ‘νΌν°μ μ μ©
const player: { name: string, age?: number } = {
name: 'kim'
};
// ERROR
if (player.age < 10) {...}
Type Alias
type μλ‘μ΄ νμ
= κΈ°μ‘΄ νμ
type ν€μλλ‘ κΈ°μ‘΄μ μ‘΄μ¬νλ νμ
μ μ΄λ¦λ§ λ°κΏμ μ¬μ©ν μ μκ² ν΄μ€λλ€.
νμ
μ μ¬μ¬μ©νκ³ μΆμ λ μ¬μ©ν μ μμ΅λλ€.
μ½λκ° κΉλνκ³ λͺ
νν΄μ§ λκΉμ§λ§ μ¬μ©νλ©΄ λ©λλ€.
μμ
// μΈν°νμ΄μ€ νλ‘νΌν°μ μ μ©
interface PaintOptions {
shape: Shape;
xPos?: number;
yPos?: number;
}
// κ°μ²΄ νλ‘νΌν°μ μ μ©
const player: { name: string, age?: number } = {
name: 'kim'
};
// ERROR
if (player.age < 10) {...}
Call Signature
(λ§€κ°λ³μ1: νμ
, λ§€κ°λ³μ2: νμ
, ...) => λ°νκ° νμ
Call Signatureλ ν¨μμ νμ
μ λ§ν©λλ€. 'ν¨μ μκ·Έλμ²'λΌκ³ λ ν©λλ€.
ν¨μμ λ§€κ°λ³μ νμ
κ³Ό λ°ν νμ
μ μμ±ν΄μ£Όμ΄μΌ ν©λλ€.
typeμ μ¬μ©νλ©΄ λ κΉλνκ² μ μν μ μμ΅λλ€.
μμ
type Add = (a: number, b: number) => number;
const add: Add = (a, b) => a + b;
+) Overloading
κ°μ μ΄λ¦μ ν¨μκ° λ§€κ°λ³μμ κ°μλ νμ
μ΄ λ€λ₯Ό λ λλ λ°ν νμ
μ΄ λ€λ₯Ό λ μ€λ²λ‘λ©μ΄λΌκ³ ν©λλ€.
μ΄λ₯Ό ν΅ν΄ μ¬λ¬ κ°μ call signatureλ₯Ό κ°μ§ μ μλ€.
μμ
// β Don't
type Add = {
(a: number, b: number): number;
(a: number, b: number, c: number): number;
};
// λλ
// β
Do: λ°ν νμ
μ΄ κ°λ€λ©΄ μ΅μ
λ λ§€κ°λ³μ μ¬μ©μ κΆμ₯ν©λλ€.
type Add = {
(a: number, b: number, c?: number): number;
};
const add: Add = (a, b, c) => {
if (c) {
return a + b + c;
}
return a + b;
};
Generic
ν¨μλ ν΄λμ€μ μ μΈ μμ μ΄ μλ, μ¬μ© μμ μ νμ
μ μ μΈν μ μλ λ°©λ²μ μ 곡ν©λλ€.
λ³μ/ν¨μλͺ
λ€μ <νμ
λ³μ μ΄λ¦> λΆμΈ ννλ‘ μ¬μ©ν©λλ€. νμ
λ³μμ μ΄λ¦μ μ무거λ ν΄λ μκ΄μμ§λ§, κ΄μ΅μ μΌλ‘ μνλ²³ λλ¬Έμ νκΈμλ‘ μ²λ¦¬νλ νΈμ
λλ€.(T, V, E, ...)
λΌμ΄λΈλ¬λ¦¬λ λ€λ₯Έ κ°λ°μκ° μ¬μ©ν κΈ°λ₯μ λ§λ€ λ μ μ©νκ² μΈ μ μμ΅λλ€.
1. μ λ€λ¦ ν¨μ νμ
ν¨μ μ€λ²λ‘λ©μ ν λ νμ©νλ κ³ μ νμ
μ΄ λ§μμ§μλ‘ μ½λμ κ°λ
μ±μ΄ λ¨μ΄μ§λλ€.
// call signature
type SuperPrint = {
// concrete type: number, boolean, string
(arr: number[]): void;
(arr: boolean[]): void;
(arr: string[]): void;
(arr: (number | boolean)[]): void;
};
const superPrint: SuperPrint = (arr) => {
arr.forEach((i) => console.log(i));
};
superPrint([1, 2, 3, 4]);
superPrint([true, true, false]);
superPrint(["a", "b", "c"]);
superPrint([1, 2, true, false]);
μ΄λ΄ λ μ λ€λ¦μ μ¬μ©νλ©΄ νμ
μ λ³μννμ¬ μ μ°νκ² ν¨μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
// λ°©λ² 1: type(λλ interface)μΌλ‘ νμ
μ§μ
type SuperPrint = {
<T>(arr: T[]): T;
};
const superPrint: SuperPrint = (arr) => arr[0];
// λ°©λ² 2: ν¨μ μ μΈμ
function superPrint<T>(arr: T[]) {
return a[0];
}
// λ°©λ² 3: ν¨μ ννμ
const superPrint = <T>(arr: T[]): T => arr[0];
const a = superPrint([1, 2, 3, 4]); // a: number
const b = superPrint([true, true, false]); // b: boolean
const c = superPrint(["a", "b", "c"]); // c: string
const d = superPrint([1, 2, true, false]); // d: number | boolean
2. μ λ€λ¦ κ°μ²΄ νμ
κ°μ²΄μλ μ λ€λ¦ νμ
μ μ μ©ν μ μμ΅λλ€.
μμ
type Player<T> = {
name: string;
extraInfo: T;
};
type ExtraInfo = {
favoriteFood: string;
};
const nico: Player<ExtraInfo> = {
name: "nico",
extraInfo: {
favoriteFood: "kimchi",
},
};
readonly
μ½κΈ° μ μ©μΌλ‘ λ§λ€μ΄μ€λλ€. μ¦, 'λΆλ³μ±'μ κ°μ§κ² λ§λ€μ΄μ£Όλ κ²μ
λλ€.
λΆλ³μ΄λ?
λ³μκ° constλ‘ μ μΈλκ±°λ readonlyλ₯Ό λͺ
μνκ³ μμΌλ©΄ μ΄κΉκ°μ νμ μ μ§ν©λλ€. μ΄λ¬ν λ³μλ₯Ό λ³κ²½ν μ μλ€λ μλ―Έλ‘ 'λΆλ³(immutable)' λ³μλΌκ³ ν©λλ€.
μ μ© λμ : λ³μ, κ°μ²΄ νλ‘νΌν°, μΈν°νμ΄μ€ νλ‘νΌν°, ν΄λμ€ νλ‘νΌν°, ν¨μμ λ§€κ°λ³μ λ±
ν΄λμ€ νλ‘νΌν°μ λν΄μ, κ°μ 곡κ°λ νκ³ μΆμ§λ§ μμ μ λͺ»νκ² νκ³ μΆμ λ readonlyλ₯Ό μ¬μ©ν μλ μμ΅λλ€.
μμ 1
type Player = {
readonly name: string;
};
const makePlayer = (name: string): Player => ({ name });
const player = makePlayer("kim");
player.name = "lee"; // ERROR
class Word {
constructor(public readonly term: string, public readonly def: string) {}
}
+) readonlyμ constμ μ°¨μ΄λ?
readonlyλ νλ‘νΌν°λ₯Ό λΆλ³μΌλ‘ λ§λ€μ΄μ€λλ€.
νμ
μ€ν¬λ¦½νΈμμ μΈν°νμ΄μ€ νλ‘νΌν°, ν΄λμ€ νλ‘νΌν°, ν¨μμ λ§€κ°λ³μ λ±μ let, const ν€μλ μμ΄ μ μΈν©λλ€.
μ΄λ¬ν κ²λ€μ λΆλ³μ±μ μ£Όλ €λ©΄ readonlyλ₯Ό μ°λ©΄ λ©λλ€.
readonlyλ λ°°μ΄κ³Ό κ°μ²΄λ₯Ό λΆλ³μΌλ‘ λ§λ€μ΄μ€λλ€.
λ°°μ΄κ³Ό 리ν°λ΄ κ°μ²΄λ constλ‘ μ μΈνλλΌλ κ·Έ μμ μμλ€μ λ³κ²½ν μ μμ΅λλ€.
λ°°μ΄μ μμμ 리ν°λ΄ κ°μ²΄μ νλ‘νΌν°μλ λΆλ³μ±μ μ£Όκ³ μΆμΌλ©΄ readonlyλ₯Ό μ¬μ©νλ©΄ λ©λλ€.
μμ
const numbers: readonly number[] = [1, 2, 3, 4];
numbers.push(5); // ERROR
function forcePure(array: readonly number[]) {
array.push(1); // ERROR
}
Tuple
μμμ νμ
μ΄ λͺ¨λ κ°μΌλ©΄ λ°°μ΄, λ€λ₯Ό μλ μμΌλ©΄ ννμ
λλ€.
νμ μ ν΄μ§ κ°μμ μμλ₯Ό κ°μ ΈμΌ νλ λ°°μ΄μ νμ
μ μ§μ ν μ μμ΅λλ€.
λ°°μ΄μ ννμ λͺ
μμ μΌλ‘ μ§μ λ νμμ λ°λΌ μμ μμλ₯Ό μ€μ ν΄μΌ νκ³ , μΆκ°λλ μμ λν ννμ λͺ
μλ νμ
λ§ μ¬μ© κ°λ₯ν©λλ€.
μμ
const player: [string, number, boolean] = ["nico", 12, true];
player[0] = 1; // ERROR
player[0] = "lynn"; // OK
// readonlyμ μ‘°ν©ν΄μ μΈ μλ μλ€.
const player: readonly [string, number, boolean] = ["nico", 12, true];
player[0] = "lynn"; // ERROR
any
λ§ κ·Έλλ‘ μ무 νμ
μ΄λ λ μ μμ΅λλ€.
νμ
μ€ν¬λ¦½νΈμ νμ
κ²μ¬λ₯Ό λΉ μ Έλμ€κ³ μΆμ λ μ¬μ©ν©λλ€.
anyλ μ¬μ€μ μλ°μ€ν¬λ¦½νΈλ₯Ό μ°λ κ²κ³Ό κ°μΌλ―λ‘ μ μ€νκ² μ¬μ©ν΄μΌ ν©λλ€.
μμ
const a: any[] = [1, 2, 3, 4];
const b: any = true;
a + b; // OK
unknown
λ³μμ νμ
μ 미리 μ μ μμ λ μ¬μ©ν©λλ€.
any μ²λΌ μ΄λ ν νμ
μ κ°μ΄λΌλ ν λΉν μ μμ§λ§, unknown νμ
μ κ°μ μ¬μ©νκΈ° μ μ λ¨Όμ νμ
νμΈ μμ
μ ν΄μΌ ν©λλ€.
anyλ³΄λ€ νμ
μμ μ±μ μ μ§νλ©΄μ λμ μΈ νμ
μ²λ¦¬κ° νμν λ μ¬μ©ν©λλ€.
μμ
let x: unknown = 10;
let y: string;
// ERROR
y = x;
// OK
if (typeof x === "string") {
y = x;
}
void
κ°μ λ°ννμ§ μλ ν¨μμ λ°ν νμ
μ
λλ€.
ν¨μμ λ°ν νμ
μΌλ‘λ§ μ¬μ©ν μ μμ΅λλ€.
function print(): void {
console.log("hi!");
}
never
λ³μμ νμ
μΌλ‘ μ¬μ©λλ©΄ μ λ λ°μν μ μλ κ°μ΄λΌλ μλ―Έμ
λλ€.
λ³΄ν΅ ν¨μμ λ°ν νμ
μΌλ‘ μ¬μ©λλ©°, ν¨μκ° κ²°μ½ κ°μ λ°ννμ§ μμμ μλ―Έν©λλ€.
μ£Όλ‘ μμΈλ₯Ό λμ§κ±°λ 무ν 루νμ κ°μ μμ
μ νλ ν¨μμμ λ°ν νμ
μΌλ‘ μ¬μ©λ©λλ€.
μμ
function fail(msg: string): never {
throw new Error(msg);
}
function fn(x: string | number) {
if (typeof x === "string") {
// do something
} else if (typeof x === "number") {
// do something else
} else {
x; // has type 'never'!
}
}
+) void vs never
voidμ never λ λ€ λ°νκ°μ΄ μλ€λ κ²μ κ°μ΅λλ€.
voidλ λ°νκ°μ μμ±νμ§ μκ³ μμ
μ μννλ λͺ©μ μμ μλ―Ένκ³ , neverλ ν¨μκ° μ μμ μΌλ‘ μ’
λ£λμ§ μμμ μλ―Έν©λλ€.
μ°Έκ³