当前位置:网站首页>TypeScript-分布式条件类型
TypeScript-分布式条件类型
2022-06-11 07:54:00 【YY小怪兽】
1.条件类型(三目运算)
判断前面一个类型是否是后面一个类型或者继承于后面一个类型
如果是就返回第一个结果, 如果不是就返回第二个结果
语法: T extends U ? X : Y;
type MyType<T> = T extends string ? string : any;
type res = MyType<boolean>//res = any
2.分布式条件类型
被检测类型是一个联合类型的时候, 该条件类型就被称之为分布式条件类型
type MyType<T> = T extends any ? T : never;
//res=string|number|boolean
type res = MyType<string | number | boolean>;
3.从T中剔除可以赋值给U的类型。 Exclude
type MyType<T, U> = T extends U ? never : T;
//res=tring | boolean
type res = MyType<string | number | boolean, number>
//res=tring | boolean
type res = Exclude<string | number | boolean, number>
4.提取T中可以赋值给U的类型。 Extract
//res = string | number
type res = Extract<string | number | boolean, number | string>
5.从T中剔除null和undefined。 NonNullable
//res=string | boolean
type res = NonNullable<string | null | boolean | undefined>
6.获取函数返回值类型。 ReturnType
//res=number
type res = ReturnType<(()=>number)>
7.获取一个类的构造函数参数组成的元组类型。 ConstructorParameters
class Person {
constructor(name:string, age:number){
}
}
//res = [name:string, age:number]
type res = ConstructorParameters<typeof Person>;
8.获得函数的参数类型组成的元组类型。 Parameters
function say(name:string, age:number, gender:boolean) {
}
//res = [string, age:number, gender:boolean]
type res = Parameters<typeof say>;
边栏推荐
- Zero foundation self-study SQL course | union joint query
- 代码设置ConstraintLayout的layout_constraintDimensionRatio
- [codeforces1019e] raining season
- [cluster] lvs+keepalived high availability cluster
- Summary of force deduction questions
- 零基础自学SQL课程 | UNION 联合查询
- 图数据库无缝集成Tushare接口
- [atcoder2307] tree game
- Zero foundation self-study SQL course | outer join external connection
- String Simulation Implementation
猜你喜欢

Getting started with bladed tutorial (video)

2022.6.7 special student simulation

Tidb Cloud est en ligne sur le marché Google Cloud pour permettre aux développeurs du monde entier d'utiliser une nouvelle pile de bases de données htap en temps réel

forEach 中 return 和 for 中 break

Tutoriel de démarrage bladed (vidéo)

wordcloud的使用

2022.6.6 特长生模拟

Implementation of stack (C language)

Understanding of Poisson distribution and Poisson process and Erlang distribution and their relations (important theories in queuing theory and operational research)

Qunhui ds918 creates m.2 SSD read / write cache
随机推荐
C language lesson 2
Rabin Miller prime test
Pycrypto packaging error
【CodeForces908H】New Year and Boolean Bridges (FWT)
Image data enhancement (translation, rotation, brightness transformation, flipping, adding Gaussian noise, scaling, cropping)
2022.6.6 特长生模拟
Database connection pool and bdutils tool
How to prepare for the new PMP syllabus exam?
运筹学导论
[IOT] project management: how to build a better cross functional team?
Black Qunhui dsm7.0.1 physical machine installation tutorial
forEach 中 return 和 for 中 break
TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者
2022.6.7 special student simulation
Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本
排序——交换排序
C# 微信上传Form-data
Simple use of string
C language - Growth Diary -01- count primes and sum
C. Managing history (greedy / hashing / thinking / good questions)