강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của 0s
0s

câu hỏi đã được viết

[Gia hạn] TypeScript tất cả trong một: Phần 1. Ngữ pháp cơ bản

Bỏ qua, loại trừ, phân tích loại trích xuất

Exclude 만들기에서 extends 구문 질문

Viết

·

350

0

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

Exclude<Animal, 'Human'>

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

U extends T가 맞지 않나요? ㅠ

 

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

typescript

Câu trả lời 2

2

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

zerocho님의 프로필 이미지
zerocho
Người chia sẻ kiến thức

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

Hình ảnh hồ sơ của 0s
0s

câu hỏi đã được viết

Đặt câu hỏi