• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

Exclude 만들기에서 extends 구문 질문

22.10.10 17:00 작성 조회수 219

0

type Exclude<T,U> = T extends U ? never : T

Exclude<Animal, 'Human'>

그런데 T가 U보다 넓은거니까

U extends T가 맞지 않나요? ㅠ

 

PS. 아.. 혹시 하나하나 읽히는거면 equal로 해석해도 되는건가요?

답변 2

·

답변을 작성해보세요.

2

로로와미미님의 프로필

로로와미미

2022.12.02

Distributive conditional types

Conditional types in which the checked type is a naked type parameter are called distributive conditional types. Distributive conditional types are automatically distributed over union types during instantiation. For example, an instantiation of T extends U ? X : Y with the type argument A | B | C for T is resolved as (A extends U ? X : Y) | (B extends U ? X : Y) | (C extends U ? X : Y).

공식문서 참고해서 혹시나 이부분고민하실 분들에게 설명드립니다

분배법칙으로 인해서 T 안에 유니온타입이 *하나씩* 대입해가면서 extends U 에 대입해가면서 포함되면 T < 쪼개진 유니온('Cat')

포함안되면 (never) never 는 유니온안에서 없는거나마찬가지입니다 즉

'Cat' | 'Dog' | never 이런결과를 얻었다면

=> 'Cat' | 'Dog' 이런 타입이되는거죠

0

Animal에 들어있는 것이 반복문처럼 하나씩 읽히는 것입니다.