[Typescript] 11. Medium - Equal

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

 

posted @ 2022-09-03 23:30  Zhentiw  阅读(24)  评论(0编辑  收藏  举报