Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2장(~아이템 12) 타입스크립트의 타입시스템 #18

Open
humonnom opened this issue Sep 25, 2022 · 0 comments
Open

2장(~아이템 12) 타입스크립트의 타입시스템 #18

humonnom opened this issue Sep 25, 2022 · 0 comments

Comments

@humonnom
Copy link
Collaborator

  • fetch 함수의 타입 선언을 타고 가다보면 Request의 타입과 값이 분리되어 모델링 되어있는 것을 볼 수 있다.(자세한 것은 아이템 8에서 다룰 예정)

  • 타입이란? 결국 값들의 집합을 의미한다.

    • 변수 value에 들어올 수 있는 값들의 집합('a', 'b', 'c')이 곧 타입임

타입은 집합이다

  • 가장 작은 집합: 공집합(never)

  • never 다음으로 작은 집합: 유닛(unit) => 'a'

  • 유닛을 여러개 묶으면: 유니온(union) => 'a' | 'b'

  • 타입체커가 하는 일은 어떤 집합이 다른 집합의 부분집합인지 확인하는 것이다.

  • 일반적인 구조적 할당 가능성 체크와 잉여 속성 체크를 구분해서 이해해야 한다.

interface a {
  name: string;
  age: number;
  password: "a";
}

interface b {
  name: string;
  password: string;
}

// intersection
type c = a & b;
type c_key = keyof (a & b);
type c_key2 = keyof a | keyof b;
// 속성은 합집합이 되고, 타입은 교집합이 된다.
// 구조적 타이핑이 적용되어 아래의 'another'와 같은 별개의 속성이 추가될 수 있다

// 타입 c는 a와 b의 교집합에 위치할 수 있는 타입이어야 함
const obj_c: c = {
  name: "jueun", // string & string
  age: 29, // number
  password: "a", // 'a' & string
  another: "", // 여기서 ts가 내는 오류는 '잉여속성체크'로, 할당이 불가능함을 의미하는 것은 아님 => 엄격한 리터럴 체크
};

const temp_obj_c: c = obj_c;
// 오류 메세지 없이 할당됩니다.
// 임시변수를 도입하면 잉여 속성 체크를 건너뛸 수 있다

@humonnom humonnom changed the title 2장 타입스크립트의 타입시스템 2장(~아이템 12) 타입스크립트의 타입시스템 Sep 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant