[Typescript] Making TypeScript Stick - 5 - Extends
typescript extends making
2023-09-14 09:00:45 时间
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
相关文章
- [Typescript] Constraining Types for Anything but null or undefined (T extends {})
- [Algorithms] Quicksort algorithm using TypeScript
- [TypeScript] Using Assertion to Convert Types in TypeScript
- [Typescript] Constraining Types for Anything but null or undefined (T extends {})
- [Typescript] Tips: Ensure that all call sites must be given value
- [Typescript] Extends and override an existing interface
- [TypeScript] instanceof and Type Guards (getPrototypeOf)
- [TypeScript] Type Definitions and Modules
- [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
- TypeScript - Classes
- TypeScript constructor signature 类型的变量赋值方式
- 使用 TypeScript 自定义装饰器给类的方法增添监听器 Listener
- vue3快速入门:学习Vue3为什么要学习TypeScript?使用 TypeScript 的 10 个理由