ํ ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ(TypeScript) ๊ฐ์ | ์ด์ ํ Winterlood - ์ธํ๋ฐ
์ด์ ํ Winterlood | , ํ๋ก ํธ์๋์ ํผํ ์ ์๋ ๋์ธ ํ์ ์คํฌ๋ฆฝํธ,์ด์ ๋ ์ ๋๋ก ์ ๋ณตํ ๋๊ฐ ์์ต๋๋ค! ๐ [์ฌ์ง]์ธํ์ฝ 2023 'ํ์ ์คํฌ๋ฆฝํธ๋ ์ ๊ทธ๋ด๊น?' ๋ฐํ์์ ๊ฐ์์ ๋๋ค. ๐ง ๋ฐฐ์
www.inflearn.com
๐ ํจ์ ํ์
// โ
ํจ์ ํ์
์ ์
// ๋ฐํ๊ฐ ํ์
์ ํ์
์คํฌ๋ฆฝํธ๊ฐ ์๋ ์ถ๋ก ๊ฐ๋ฅ → ์๋ต ๊ฐ๋ฅ
function func(a: number, b: number): number {
return a + b;
}
// โ
ํ์ดํ ํจ์ ํ์
์ ์
const add = (a: number, b: number): number => a + b;
// โ
๊ธฐ๋ณธ๊ฐ์ด ์ค์ ๋ ๋งค๊ฐ๋ณ์
// โ ์ ํ์ ๋งค๊ฐ๋ณ์๋ ํญ์ ํ์ ๋งค๊ฐ๋ณ์ ๋ค์ ์์นํด์ผ ํจ
function introduce(name = "๊ฐ์ฐ์", age: number, tall?: number) {
console.log(`name : ${name}`);
// ์ ํ์ ๋งค๊ฐ๋ณ์๋ undefined์ผ ์ ์์ผ๋ฏ๋ก ํ์
์ขํ๊ธฐ๋ฅผ ํตํด ์ฌ์ฉ
if (typeof tall === 'number') {
console.log(`tall : ${tall + 10}`);
}
}
introduce("๊ฐ์ฐ์", 24, 160);
introduce("๊ฐ๋ง๋ฆฌ", 5);
// โ
๋๋จธ์ง ๋งค๊ฐ๋ณ์ (rest parameter)
function getSum(...rest: number[]) {
let sum = 0;
rest.forEach((it) => (sum += it));
return sum;
}
getSum(1, 2, 3);
getSum(1, 2, 3, 4, 5);
๐ ํจ์ ํ์ ํํ์๊ณผ ํธ์ถ ์๊ทธ๋์ณ
// โ
ํจ์ ํ์
ํํ์ (Function Type Expression)
// ์ฌ๋ฌ ํจ์๊ฐ ๊ฐ์ ํ์
๊ตฌ์กฐ๋ฅผ ๋ฐ๋ฅผ ๊ฒฝ์ฐ ์ค๋ณต ์ฝ๋ ์ ๊ฑฐ ๊ฐ๋ฅ
// ํจ์ ๊ตฌํ๊ณผ ํ์
์ ์ธ์ ๋ถ๋ฆฌํ ์ ์์ด ๊ฐ๋
์ฑ๊ณผ ์ ์ง๋ณด์์ฑ ํฅ์
type Operation = (a: number, b: number) => number;
const add: Operation = (a, b) => a + b;
const sub: Operation = (a, b) => a - b;
const multiply: Operation = (a, b) => a * b;
const divide: Operation = (a, b) => a / b;
// โ
ํธ์ถ ์๊ทธ๋์ฒ (Call Signature)
// ๊ฐ์ฒด ํํ์ ํ์
์ ์ธ ์์์ ํจ์๋ฅผ ์ ์ํ๋ ๋ฌธ๋ฒ
// ํจ์ ํ์
ํํ์๊ณผ ์ญํ ์ ๊ฐ์ง๋ง, ๊ฐ์ฒด์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ ์ถ๊ฐ์ ์ธ ์ฅ์ ๋ณด์
type Operation2 = {
(a: number, b: number): number;
name: string; // ํธ์ถ ์๊ทธ๋์ฒ ์์ ์ผ๋ฐ ์์ฑ์ ์ถ๊ฐํ๋ฉด 'ํ์ด๋ธ๋ฆฌ๋ ํ์
'์ด ๋จ
};
const add2: Operation2 = (a, b) => a + b;
const sub2: Operation2 = (a, b) => a - b;
const multiply2: Operation2 = (a, b) => a * b;
const divide2: Operation2 = (a, b) => a / b;
๐ ํจ์ ํ์ ์ ํธํ์ฑ
// โ
ํจ์ ํ์
์ ํธํ์ฑ
// "์ด ํจ์ ํ์
์ ๋ค๋ฅธ ํจ์ ํ์
์ฒ๋ผ ์จ๋ ๊ด์ฐฎ์๊น?"๋ฅผ ํ๋จํ๋ ๊ธฐ์ค
// โ
๊ธฐ์ค 1: ๋ฐํ๊ฐ ํ์
์ด ํธํ๋๋๊ฐ?
// โ ๋ ๊ตฌ์ฒด์ ์ธ ๊ฐ(ํ์ ํ์
)์ ๋ ๋์ ๊ฐ(์์ ํ์
)์ ๋ค์ด๊ฐ ์ ์์
type A = () => number;
type B = () => 10;
let a: A = () => 10;
let b: B = () => 10;
a = b; // OK: a()๋ ์ค์ b()๋ฅผ ํธ์ถ → ํญ์ 10์ ๋ฐํํ๋, number๋ฅผ ๊ธฐ๋ํ๋ a์๋ ๋ฌธ์ ์์
// b = a; // ERROR: b()๋ ์ค์ a()๋ฅผ ํธ์ถ → 10์ด ์๋ ์๋ ์์ (์: 999, -1, ๋ฑ๋ฑ)
// โ
๊ธฐ์ค 2: ๋งค๊ฐ๋ณ์๊ฐ ํธํ๋๋๊ฐ?
// โ ๋งค๊ฐ๋ณ์๋ ๋ฐ๋๋ก, "๋ ๋์ ํ์
"์ "๋ ์ข์ ํ์
"์ผ๋ก ๋์
ํ๋ ๊ฒ์ ๋ถ๊ฐ
// 2-1. ๋งค๊ฐ๋ณ์์ ๊ฐ์๊ฐ ๊ฐ์ ๋
type C = (value: number) => void;
type D = (value: 10) => void;
let c: C = (value) => {};
let d: D = (value) => {};
// c = d; // ERROR: c(20)๋ ์ค์ d(20)๋ฅผ ํธ์ถ → d๋ 10 ์๋๋ฉด ๋ชป ๋ฐ์์ ์ํ
d = c; // OK: d(10)๋ ์ค์ c(10)์ ํธ์ถ → c๋ number ์ ์ฒด OK, ๋ฌธ์ ์์
// 2-2. ๋งค๊ฐ๋ณ์์ ๊ฐ์๊ฐ ๋ค๋ฅผ ๋
type Func1 = (a: number, b: number) => void;
type Func2 = (a: number) => void;
let func1: Func1 = (a, b) => {};
let func2: Func2 = (a) => {};
func1 = func2; // OK: func1(1, 2)๋ ์ค์ func2(1)๋ฅผ ํธ์ถ → b๋ ๋ฌด์๋๋ฏ๋ก ๋ฌธ์ ์์
// func2 = func1; // ERROR: func2(1)๋ ์ค์ func1(1, undefined)๋ฅผ ํธ์ถ → b๋ ๋๋ฝ๋ ์ ์์ด ์ํ
๐ ํจ์ ์ค๋ฒ๋ก๋ฉ
// โ
ํจ์ ์ค๋ฒ๋ก๋ฉ
// → ํ๋์ ํจ์ ์ด๋ฆ์ผ๋ก, ๋งค๊ฐ๋ณ์ ๊ฐ์๋ ํ์
์ ๋ฐ๋ผ ์ฌ๋ฌ ๋ฒ์ ์ ์ ์ํ๋ ๋ฐฉ์
// ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ง์ X, ์ค์ง ํ์
์คํฌ๋ฆฝํธ๋ง ์ง์
// ์ค๋ฒ๋ก๋ ์๊ทธ๋์ฒ: "์ด๋ฐ ๋ฒ์ ์ด ์กด์ฌํด!"๋ผ๊ณ ์ ์ธ๋ง ํด์ฃผ๋ ๋ถ๋ถ
function func(a: number): void;
function func(a: number, b: number, c: number): void;
// ๊ตฌํ ์๊ทธ๋์ฒ: ์ค์ ํจ์ ๊ตฌํ
function func(a: number, b?: number, c?: number) {
if (typeof b === 'number' && typeof c === 'number') {
console.log(a + b + c);
} else {
console.log(a * 20);
}
};
๐ ์ฌ์ฉ์ ์ ์ ํ์ ๊ฐ๋
// โ
์ฌ์ฉ์ ์ ์ ํ์
๊ฐ๋
type Dog = {
name: string;
isBark: boolean;
};
type Cat = {
name: string;
isScratch: boolean;
};
type Animal = Dog | Cat;
// ์ฌ์ฉ์ ์ ์ ํ์
๊ฐ๋
function isDog(animal: Animal): animal is Dog { // animal is Dog: ํด๋น ํจ์๊ฐ true๋ฅผ ๋ฐํํ๋ฉด, ํ์
์คํฌ๋ฆฝํธ๋ animal์ Dog๋ก ๊ฐ์ฃผ
return (animal as Dog).isBark !== undefined;
}
function isCat(animal: Animal): animal is Cat { // animal is Cat: ํด๋น ํจ์๊ฐ true๋ฅผ ๋ฐํํ๋ฉด, ํ์
์คํฌ๋ฆฝํธ๋ animal์ Cat์ผ๋ก ๊ฐ์ฃผ
return (animal as Cat).isScratch !== undefined;
}
// ํ์
๊ฐ๋๋ฅผ ํ์ฉํด Animal์ ํ์
์ ์์ ํ๊ฒ ์ขํ ์ฌ์ฉ
function warning(animal: Animal) {
if (isDog(animal)) {
console.log("์์ ์ฃผ์")
} else if (isCat(animal)) {
console.log("๋ถ์ ์ฃผ์")
}
}
โฌ๏ธ ๋ค์ ๋ด์ฉ์ ์๋ ๊ธ์ ์ฐธ๊ณ
์ธํ๋ฐ ๊ฐ์ - ํ ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ (์ธํฐํ์ด์ค)
ํ ์ ํฌ๊ธฐ๋ก ์๋ผ๋จน๋ ํ์ ์คํฌ๋ฆฝํธ(TypeScript) ๊ฐ์ | ์ด์ ํ Winterlood - ์ธํ๋ฐ์ด์ ํ Winterlood | , ํ๋ก ํธ์๋์ ํผํ ์ ์๋ ๋์ธ ํ์ ์คํฌ๋ฆฝํธ,์ด์ ๋ ์ ๋๋ก ์ ๋ณตํ ๋๊ฐ ์์ต๋๋ค! ๐ [์ฌ์ง]
isliife2.tistory.com