인프런 커뮤니티 질문&답변

0S님의 프로필 이미지

작성한 질문수

[리뉴얼] 타입스크립트 올인원 : Part1. 기본 문법편

Omit, Exclude, Extract 타입 분석

Exclude 만들기에서 extends 구문 질문

22.10.10 17:00 작성

·

267

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. 23:22

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

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

2022. 10. 10. 22:22

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

0S님의 프로필 이미지

작성한 질문수

질문하기