首页 > 其他分享 >[Typescript] 11. Medium - Equal

[Typescript] 11. Medium - Equal

时间:2022-09-03 23:46:28浏览次数:54  
标签:11 Typescript type Equal Medium extends true

Implement the Equal<T, U>

For example:

type isEqual = Equal<1, 1> // true

Idea: 

Parameter type: 

<P>(x: P) => any

Check P extends T ? 1: 2

Then check P extends U ? 1 : 2

Then see whether 1 extends 1 or 2 extends 2?  then true

If 1 extends 2 or 2 extends 1 then false

type Equal<T, U> = 
  (<P>(x: P) => P extends T ? 1: 2) extends 
  (<P>(x: P) => P extends U ? 1: 2) 
    ? true
    : false

 

标签:11,Typescript,type,Equal,Medium,extends,true
From: https://www.cnblogs.com/Answer1215/p/16653980.html

相关文章