๐ซ ์ง๋
4. ํจ์์ ํ์ (3/5)
- ํจ์ ํ์
- ํจ์ ํ์ ํํ์๊ณผ ํธ์ถ ์๊ทธ๋์ฒ
- ํจ์ ํ์ ์ ํธํ์ฑ
๐ฑ ์์ฝ
ํจ์ ํ์
/**
* ํจ์ ํ์
์ ์
*/
// ํจ์๋ฅผ ์ค๋ช
ํ๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ
// (์ด๋ค ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๊ณ , ์ด๋ค ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋์ง ์ด์ผ๊ธฐ)
// => ์ด๋ค [ํ์
์] ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๊ณ , ์ด๋ค [ํ์
์] ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๋์ง ์ด์ผ๊ธฐ
// function func(a: number, b: number): number
function func(a: number, b: number) {
return a + b;
}
/**
* ํ์ดํ ํจ์๋ฅผ ์ ์ํ๋ ๋ฒ
*/
// (a: number, b: number): number๋
// (a: number, b: number)๋ก ์๋ต ๊ฐ๋ฅ
const add = (a: number, b: number): number => a + b;
/**
* ํจ์์ ๋งค๊ฐ๋ณ์
*/
// ์์ ํ์ ๋งค๊ฐ๋ณ์๊ฐ ๋จผ์ ์ค๊ณ ๋ง์ง๋ง์ ์ ํ์ ๋งค๊ฐ๋ณ์(=?)๊ฐ ์์ผํจ
function introduce(name = "marvin", tall?: number) {
console.log(`name: ${name}`);
if (typeof tall === "number") {
console.log(`tall: ${tall}+ 1`);
}
}
introduce("marvin", 185);
introduce("marvin");
ํจ์ ํ์ ํํ์๊ณผ ํธ์ถ ์๊ทธ๋์ฒ
/**
* ํจ์ ํ์
ํํ์ Function Type Expresstion
*/
type Operation = (a: number, b: number) => number;
// const add:Operation = (a, b) => a + b; ๋ฐ์ add๋ ๊ฐ์ ๋ด์ฉ
const add: (a: number, b: number) => number = (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 Signiture
*/
type Operation2 = {
(a: number, b: number): number;
name: string;
// -> ๊ฐ์ฒด๋ก๋ ์ฐ๊ณ ํจ์๋ก๋ ์ธ ์ ์๋ hybrid type
// -> ํจ์๋ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅ
};
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;
// add2();
// add2.name;
ํจ์ ํ์ ํธํ์ฑ
/**
* ํจ์ ํ์
ํธํ์ฑ
* ํน์ ํจ์ ํ์
์ ๋ค๋ฅธ ํจ์ ํ์
์ผ๋ก ์ทจ๊ธํด๋ ๊ด์ฐฎ์๊ฐ๋ฅผ ํ๋จํ๋
* 1. ๋ฐํ๊ฐ์ ํ์
์ด ํธํ๋๋๊ฐ?
* 2. ๋งค๊ฐ๋ณ์์ ํ์
์ด ํธํ๋๋๊ฐ?
*/
// 1. ๋ฐํ๊ฐ์ ํ์
์ด ํธํ๋๋๊ฐ?
type A = () => number;
type B = () => 10;
let a: A = () => 10;
let b: B = () => 10;
a = b; // b = a;
// - B <= A
// - number literal type <= number type
// - ๋ฐํ๊ฐ ๊ธฐ์ค์ด๋ฉด ๋ค์ด์บ์คํ
X, ์
์บ์คํ
๋ง ๊ฐ๋ฅ
// 2. ๋งค๊ฐ๋ณ์์ ํ์
์ด ํธํ๋๋๊ฐ?
// 2-1. ๋งค๊ฐ๋ณ์์ ๊ฐฏ์๊ฐ ๊ฐ์ ๋
type C = (value: number) => void;
type D = (value: 10) => void;
let c: C = (value) => {};
let d: D = (value) => {};
// c = d;
// - C <= D
// - number type <= number literal type
// - ๋งค๊ฐ๋ณ์ ๊ธฐ์ค์ด๋ฉด ์
์บ์คํ
X
d = c;
/* ++ ๊ฐ์ฒด๋ก ์์ */
type Animal = {
name: string;
};
type Dog = {
name: string;
color: string;
};
let animalFunc = (animal: Animal) => {
console.log(animal.name);
};
let dogFunc = (dog: Dog) => {
console.log(dog.name);
console.log(dog.color);
};
// animalFunc = dogFunc;
// - Animal <= Dog
// - Dogํ์
์ Animalํ์
์ ์๋ธ ํ์
์ด ๋๋ฌธ์ Animal์ด ๊ฐ์ง ํ์
์ ๋ค ๊ฐ์ง๊ณ ์์.
// - ๊ทธ๋ ์ง๋ง Animal์ Dogํ์
์ด ๊ฐ์ง ํ์
์ ๋ค ๊ฐ์ง๊ณ ์์ง ์๊ธฐ ๋๋ฌธ์ ์๋๋ค
// - ๋งค๊ฐ๋ณ์ ๊ธฐ์ค์ผ ๊ฒฝ์ฐ์๋ ๋ค์ด ์บ์คํ
๋ง ๊ฐ๋ฅํ๋ค.
dogFunc = animalFunc;
// 2-2. ๋งค๊ฐ๋ณ์์ ๊ฐฏ์๊ฐ ๋ค๋ฅผ ๋
// - ํ์
๊ฐ์ ๊ฒ ์์ด์ผ ๊ฐ๋ฅ
// - type Func1 = (a: string, b: number) => void; ์ด๋ฌ๋ฉด ๋ชปํจ
type Func1 = (a: number, b: number) => void;
type Func2 = (a: number) => void;
let func1: Func1 = (a, b) => {};
let func2: Func2 = (a) => {};
func1 = func2; // func2 = func1;
// - Func2 <= Func1
// - Func2์ ๋งค๊ฐ๋ณ์์ ๊ฐฏ์๋ 1๊ฐ, Func1์ 2๊ฐ์ธ๋ฐ
// - 1๊ฐ <= 2๊ฐ ํ๋ ค๋ ํธํ์ด ์๋๋ ๊ฒ
๐ ๊ณผ์
Q1. ์๋์ ์ฝ๋๋ฅผ ์์ฑํด ์ค๋ฅ๋ฅผ ์ ๊ฑฐํ์ธ์.
function introduce(name = "์ด์ ํ", tall?: number){
if(!tall){
console.log(`์๋
ํ์ธ์ ${name}์
๋๋ค.`)
} else {
console.log(`์๋
ํ์ธ์ ${name}์
๋๋ค. ํค๋ ${tall}์
๋๋ค.`)
}
}
Q2. ์๋์ ์ฝ๋๋ฅผ ์์ฑํด ์ค๋ฅ๋ฅผ ์ ๊ฑฐํ์ธ์.
type Func = (a: number, b: string) => boolean; 