TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day7

2024. 8. 9. 00:54ยทlecture recap

๐Ÿซ– ์ง„๋„

 

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; 

'lecture recap' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day9  (0) 2024.08.09
TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day8  (0) 2024.08.09
TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day6  (0) 2024.08.09
TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day5  (1) 2024.08.09
TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day4  (0) 2024.08.09
'lecture recap' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day9
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day8
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day6
  • TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day5
nuew
nuew
๐Ÿคธ ์žฌ์ฃผ ๋„˜๋Š” ์ค‘
  • nuew
    bloggg. . .๐Ÿฆ–๐Ÿ’ฅ
    nuew
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (88)
      • issue (10)
      • baekjoon (41)
      • lecture recap (11)
      • What I Learn (26)
      • retrospective (0)
      • maeil-mail (0)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    css
    Algorithm
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ํ•œ์ž…ํฌ๊ธฐ๋กœ์ž˜๋ผ๋จน๋Š”ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    JavaScript
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
    zustand
    modal
    media-query
    what i learn
    Baekjoon
    TailwindCSS
    ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
    TypeScript
    ๋ฐฑ์ค€
    Study
    js
    Node.js
    issue
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
nuew
TS: ํ•œ์ž…ํฌ๊ธฐ๋กœ ์ž˜๋ผ๋จน๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ฑŒ๋ฆฐ์ง€_day7
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”