2 ๋ถ„ ์†Œ์š”

Typescript ๊ธฐ์ดˆ2 ๐Ÿ”‘

Literal Type

let ์ˆซ์ž: 123;
์ˆซ์ž: 456; // ERROR

์ •ํ•ด์ง„ ๊ฐ’๋งŒ ๋“ค์–ด์˜ค๊ฒŒ ํƒ€์ž…์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

let a = (x: '๊ฐ€์œ„' | '๋ฐ”์œ„' | '๋ณด'): ('๊ฐ€์œ„' | '๋ฐ”์œ„' | '๋ณด')[] => {
  return ['๊ฐ€์œ„', '๋ณด'];
};

ํ•จ์ˆ˜๋„ ๊ฐ€๋Šฅ

var ์ž๋ฃŒ = {
  name: 'kim',
};

function ๋‚ดํ•จ์ˆ˜(a: 'kim') {}

๋‚ดํ•จ์ˆ˜(์ž๋ฃŒ.name); // ERROR

์—๋Ÿฌ๊ฐ€ ๋œจ๋Š” ์ด์œ ๋Š” a: 'kim'์˜ ๋œป์€ โ€˜kimโ€™์ด๋ผ๋Š” type๋งŒ ๋ฐ›๊ฒ ๋‹ค๋Š”๊ฑฐ๋‹ค.

๋”ฐ๋ผ์„œ ๊ฐ’์ด โ€˜kimโ€™์ธ ์ž๋ฃŒ.name์„ ๋„ฃ์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.

var ์ž๋ฃŒ = {
  name: 'kim',
} as const;

function ๋‚ดํ•จ์ˆ˜(a: 'kim') {}

๋‚ดํ•จ์ˆ˜(์ž๋ฃŒ.name); // ERROR

์ด๋ ‡๊ฒŒ as const๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Literal Type์ฒ˜๋Ÿผ ์ง€์ •ํ•ด์ฃผ๋Š”๋ฐ

name: โ€˜kimโ€™ ์ด๋ ‡๊ฒŒ ํƒ€์ž…์ด ์ง€์ •๋œ๋‹ค.

  1. ํ‚ค๊ฐ’์„ ํƒ€์ž…์œผ๋กœ ์ง€์ •ํ•ด์คŒ
  2. ํ‚ค๋ฅผ readonly๋กœ ์ง€์ •ํ•˜์—ฌ ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๊ฒŒํ•ด์คŒ




methods ์•ˆ์— ํƒ€์ž…์ง€์ •ํ•˜๊ธฐ

type Member = {
  name: string;
  age: number;
  plusOne: (x: number) => number;
  changeName: () => void;
};

let ํšŒ์›์ •๋ณด: Member = {
  name: 'kim',
  age: 30,
  plusOne(x) {
    return x + 1;
  },
  changeName: () => {
    console.log('์•ˆ๋…•');
  },
};
ํšŒ์›์ •๋ณด.plusOne(1);
ํšŒ์›์ •๋ณด.changeName();

๋ฉ”์†Œ๋“œ์˜ ํƒ€์ž…์ง€์ •

type CutType = (x: string) => string;
type RemoveDash = (x: string) => number;

let cutZero: CutType = (x) => {
  let result = x.replace(/^0+/, '');
  return result;
};
let removeDash: RemoveDash = (x) => {
  let result = x.replace(/-/g, '');
  return parseFloat(result);
};

function allFunction(a: string, func1: CutType, func2: RemoveDash): void {
  let result = func1(a);
  let result2 = func2(result);
  console.log(result2);
}
allFunction('010-1111-2222', cutZero, removeDash);




class

class Person {
  name: string; // ํ•„๋“œ๊ฐ’
  constructor(a: string) {
    this.name = a;
  }
}

ํ•„๋“œ๊ฐ’์„ ๋ฏธ๋ฆฌ ์„ ์–ธํ•ด์ฃผ์–ด์•ผ ์‚ฌ์šฉ๊ฐ€๋Šฅ

class Word {
  num;
  str;

  constructor(...param) {
    let ์ˆซ์ž๋“ค: number[] = [];
    let ๋ฌธ์ž๋“ค: string[] = [];

    param.forEach((i) => {
      if (typeof i === 'string') {
        ๋ฌธ์ž๋“ค.push(i);
      } else {
        ์ˆซ์ž๋“ค.push(i);
      }
    });

    this.num = ์ˆซ์ž๋“ค;
    this.str = ๋ฌธ์ž๋“ค;
  }
}

let obj = new Word('kim', 3, 5, 'park');
console.log(obj.num); //[3,5]
console.log(obj.str); //['kim', 'park']

์˜ˆ์‹œ




interface

interface Square {
  color: string;
  width: number;
}
//type Square = { color: string; width: number };

let square: Square = { color: 'red', width: 100 };

object ํƒ€์ž…์ง€์ •์‹œ interface ์‚ฌ์šฉ๊ฐ€๋Šฅ

interface Student {
  name: string;
}
interface Teacher {
  name: string;
  age: number;
}

//--------------------------------------------------------------------------------name ๊ฐ™์œผ๋ฏ€๋กœ ํ•ฉ์นœ๋‹ค

interface Student {
  name: string;
}
interface Teacher extends Student {
  age: number;
}

class ๋ฌธ๋ฒ•๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ

type Student = { name: string };
type Teacher = { name: string; age: number };

//--------------------------------------------------------------------------------name ๊ฐ™์œผ๋ฏ€๋กœ ํ•ฉ์นœ๋‹ค

type Student = { name: string };
type Teacher = { age: number } & Student;

type๋„ ์œ„์™€๊ฐ™์ด ์‚ฌ์šฉ๊ฐ€๋Šฅ

interface Cart {
  product: string;
  price: number;
}

let ์žฅ๋ฐ”๊ตฌ๋‹ˆ: Cart[] = [
  { product: '์ฒญ์†Œ๊ธฐ', price: 7000 },
  { product: '์‚ผ๋‹ค์ˆ˜', price: 800 },
];

๋ฐฐ์—ด์•ˆ์˜ ๊ฐ์ฒด๋งŒ๋“ค๊ธฐ

interface MathObj {
  plus: (a: number, b: number) => number;
  minus: (a: number, b: number) => number;
}

let ์˜ค๋ธŒ์ ํŠธ: MathObj = {
  plus(a, b) {
    return a + b;
  },
  minus(a, b) {
    return a - b;
  },
};

interface ๋ฉ”์„œ๋“œ

Interface vs Type

Interface๋Š” ์ค‘๋ณต์„ ์–ธ ๊ฐ€๋Šฅ Type ์ค‘๋ณต์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ

interface Student {
  name: string;
}
interface Student {
  age: number;
}

์ด๋ ‡๊ฒŒ ์ค‘๋ณต์„ ์–ธํ•˜๋ฉด

type Student = { name: string, age: number}๊ณผ ๊ฐ™๋‹ค

์ฆ‰, ์ž๋™์œผ๋กœ extendsํ•ด์ค€๋‹ค.


์—…๋ฐ์ดํŠธ: