[한입 TS 챌린지] Day 5

woodstock·2023년 12월 11일
0
post-thumbnail

강의 진도

  • 객체 타입의 호환성
  • 대수 타입
  • 타입 추론

관련 게시글

진행 날짜

2023년 12월 9일

Mission

Quiz 1.

아래 코드의 변수 a,b,c,d,e의 타입은 각각 어떻게 추론될까요?

let a = 10;
const b = 20;
const c = [1, 2];
const d = [1, "hello", true];
const e = [1, 2, 3] as const;

정답

type A = number;
type B = 20;
type C = number[];
type D = (number | string | boolean)[];
type E = [1, 2, 3];


Quiz 2.

다음 요구사항을 만족하는 Animal, DogCat(개냥이) 타입을 완성하세요

  • Animal 타입은 Dog 타입일 수도 Cat 타입일 수도 있습니다.
  • DogCat 타입은 Dog이자 Cat입니다.
type Dog = {
  name: string;
  color: string;
};

type Cat = {
  name: string;
  age: number;
};

type Animal = never;
type DogCat = never;

정답

type Animal = Dog | Cat;
type DogCat = Dog & Cat;

profile
해내는 사람

0개의 댓글