Typescript [5] - 타입

lionloopy·2023년 5월 6일
0

타입스크립트

목록 보기
5/8

any타입

: 어떠한 타입이든 모두 ok!
=> 작업중인 코드의 타입 명시가 어려운 경우에만 제한적으로 사용

유니언타입

: 제한된 타입들을 동시에 명시하고 싶을 때

let someValue : number | string

타입 별칭

 let totalCost: number;
  let orderID: number | string;
  const calculate = (price: number | string, qty: number): void => {};

  const findOrderId = (
    customer: { customerId: number | string; name: string },
    productId: number | string
  ): number | string => {
    return orderID;
  };

이렇게 number | string 타입이 중복될 때 타입 자체를 코드로 만들어 재사용할 수 있다.

  type StrOrNum = number | string;
  let totalCost: number;
  let orderID: StrOrNum;
  const calculate = (price: StrOrNum, qty: number): void => {};

  const findOrderId = (
    customer: { customerId: StrOrNum; name: string },
    productId: StrOrNum
  ): StrOrNum => {
    return orderID;
  };
  1. type 선언
  2. type 이름 지정
  3. 값 할당 후
  4. 사용

타입가드

: 코드 검증을 수행하는 것.

profile
Developer ʕ ·ᴥ·ʔ ʕ·ᴥ· ʔ

0개의 댓글