[Typescript] Making TypeScript Stick - 5 - Extends

Let’s study a few examples of extends scenarios and see if we can figure out whether it will evaluate to true or false

64 extends number

.

.

.

Answer:

查看代码
 true

 

number extends 64

.

.

.

Answer:

查看代码
 false

 

 

string[] extends any

.

.

.

Answer:

查看代码
 true

// does any contians string[] --- yes it is

 

 

string[] extends any[]

.

.

.

Answer:

查看代码
 true

// does any[] contains string[] --- yes it is

 

 

never extends any

.

.

.

.

Answer:

查看代码
 true

// yes, any can contain never type

 

 

 

any extends any

.

.

.

Answer:

查看代码
 true

 

 

Date extends {new (...args: any[]): any }

.

.

.

.

Answer:

false

// Date is instance type
// does not extends constructor() 

 

(typeof Date) extends {new (...args: any[]): any }

.

.

.

.Answer:

查看代码
 true

// typeof Date is a constructor type
posted @ 2022-08-15 01:47  Zhentiw  阅读(15)  评论(0编辑  收藏  举报