InflearnTS Chapter 7~9 - YDP-SPLOUNGE-CLUB/typescript-study GitHub Wiki

νƒ€μž… μ—μΌλ¦¬μ–ΈμŠ€

μΈν„°νŽ˜μ΄μŠ€μ™€ νƒ€μž…κ³Όμ˜ 차이가 μžˆμ§€λ§Œ μ’€ 더 객체지ν–₯적으둜 ν”„λ‘œκ·Έλž˜λ°μ„ ν•  λ•Œμ—λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” νŽΈμ΄λ‹€.

νƒ€μž…κ³Ό μΈν„°νŽ˜μ΄μŠ€κ°€ μ™„λ²½ν•˜κ²Œ λΆ„λ¦¬λ˜μ–΄ μžˆλŠ” 것이 μ•„λ‹Œ ν‘œν˜„μ˜ λ°©μ‹μ˜ 차이라고 μƒκ°ν•˜λ©΄ μ’‹λ‹€.

type Animal = { breath: true };
type Mammalia = Animal & { breed: true };
type Human = Mammalia & { think: true };
// 상속 κΈ°λŠ₯ 

const me: Human = { breath: true, think: true, breed: true }

interface A {
    breath: true
}

interface B extends A {
    breed: true
}

// μΈν„°νŽ˜μ΄μŠ€μ— type 을 상속할 μˆ˜λ„ μžˆλ‹€.

interface C extends Human {
    
}

ν•˜μ§€λ§Œ μΈν„°νŽ˜μ΄μŠ€λŠ” 같은 μ΄λ¦„μœΌλ‘œ 선언이 κ°€λŠ₯ν•˜λ‹€. μ—¬λŸ¬λ²ˆ μ„ μ–Έν•  λ•Œλ§ˆλ‹€ 합쳐진닀.

interface, enum, type 같은 경우 λ³€μˆ˜μ— I,E,Tλ₯Ό 맨 μ•žμ— λΆ™μ΄μ§€λ§Œ μš”μ¦˜μ—λŠ” μ•ˆλΆ™μ΄λŠ” 좔세라고 ν•œλ‹€?

νƒ€μž…μ„ μ§‘ν•©μœΌλ‘œ μƒκ°ν•˜μž.

쒁은 νƒ€μž…μ—μ„œ 넓은 νƒ€μž…μœΌλ‘œ λŒ€μž…μ΄ κ°€λŠ₯ν•˜λ‹€. 넓은 νƒ€μž…μ—μ„œ 쒁은 νƒ€μž…μœΌλ‘œ λŒ€μž…μ΄ λΆˆκ°€λŠ₯ν•˜λ‹€.

// 넓은 νƒ€μž…
type A = string | number;
// 쒁은 νƒ€μž…
type B = string;

κ°μ²΄λŠ” μƒμ„Έν• μˆ˜λ‘ 쒁은 νƒ€μž…μ΄λ‹€.

img

// 넓은 νƒ€μž…
type A = { name: string };
type B = { age: number };
// 쒁은 νƒ€μž…
// type C = { name: string, age: number };
type AB = A | B;
type C = A & B;

// CλŠ” 쒁은 νƒ€μž…μ΄λ©° AB λŠ” 넓은 νƒ€μž…μ΄λ‹€. κ·ΈλŸ¬λ―€λ‘œ cλŠ” abλ₯Ό λŒ€μž…ν•  수 μ—†λ‹€.
const ab1: AB = { name: 'name' };
const c1: C = ab1; // Error

// ABλŠ” 넓은 νƒ€μž…μ΄λ©° CλŠ” 쒁은 νƒ€μž…μ΄λ‹€.
const c2: C = { name: 'name', age: 1 };
const ab2: AB = c2; // OK

const ab3: AB = { name: 'name' };

// C λΌλŠ” 쒁은 νƒ€μž…μ— married λΌλŠ” 넓은 νƒ€μž…μ„ λŒ€μž…ν–ˆμ§€λ§Œ μ—λŸ¬κ°€ λ‚˜μ˜¨λ‹€.
// 객체의 λ¦¬ν„°λŸ΄μ„ 검사할 λ•Œ μž‰μ—¬ 속성 κ²€μ‚¬λΌλŠ” μΆ”κ°€ 검사λ₯Ό ν•œλ‹€.
// λ³€μˆ˜λ‘œ λ”°λ‘œ λΉΌμ„œ μ •μ˜λ₯Ό ν•΄μ€€λ‹€λ©΄ μž‰μ—¬ 속성 검사λ₯Ό μ‹€ν–‰ν•˜μ§€ μ•ŠλŠ”λ‹€.
const c3: C = { name: 'name', age: 1, married: false }; // married False is not defined

void 의 μ‚¬μš©λ²•

return 값이 μ—†λŠ” 것을 λœ»ν•œλ‹€.

// ν•˜μ§€λ§Œ undefined λŠ” λ°˜ν™˜ κ°€λŠ₯ν•˜λ‹€.
function a(): void {
    return undefined;
}

리턴값이 void 일 κ²½μš°μ™€ λ§€κ°œλ³€μˆ˜κ°€ void 인 경우, λ©”μ„œλ“œλ‘œ μ„ μ–Έν•œ void, μ„Έ κ°€μ§€λ‘œ λ‚˜λˆŒ 수 μžˆλ‹€.

ν•¨μˆ˜μ˜ 직접적인 리턴값이 void 의 κ²½μš°μ—λ§Œ return 값을 아무것도 λ³΄λ‚΄μ„œ μ•ˆλ˜λ©°. λ©”μ„œλ“œμ˜ void λŠ” 리턴값을 μ‚¬μš©ν•˜μ§€ μ•Šκ² λ‹€λŠ” 의미

function a(callback: () => void): void {
    
}

a(() => {
    return '3';
});

interface Human {
    talk: () => void;
};

const human: Human = {
    talk() { return '123' }
};

const aa = human.talk() as number; // Error
const bb = human.talk() as unknown as number // Error
// ν•¨μˆ˜μ˜ νƒ€μž…μ„ μ„ μ–Έν•˜κ³  κ΅¬ν˜„λ„λ₯Ό λ§Œλ“€μ§€ μ•ŠμœΌλ©΄ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.
// Function implementation is missing or not immediately following the declaration
// function forEach(arr: number[], callback: (el: number) => undefined): void;

// declare λ₯Ό μ„ μ–Έν•˜λ©΄ ν•΄λ‹Ή 였λ₯˜λŠ” 사라진닀. λ‹€λ§Œ μžλ°”μŠ€ν¬λ¦½νŠΈ λ³€ν™˜μ‹œ μ½”λ“œκ°€ μ‚­μ œλœλ‹€.
// undefined , void 의 차이 λ‹€μ‹œν•œλ²ˆ λ§ν•˜λ©΄ λ©”μ„œλ“œ voidλŠ” 리턴 값을 μ‚¬μš©ν•˜μ§€ μ•Šκ² λ‹€λŠ” 뜻.
declare function forEach2(arr: number[], callback: (el: number) => undefined): void;

let target: number[] = [];
forEach2([1,2,3], el => target.push(el)); // Error push 의 return 값은 number