TypeScript内置类型一览(Record<string,any>等等)(中)

简介: TypeScript内置类型一览(Record<string,any>等等)

TypeScript内置类型一览(Record<string,any>等等)(上):https://developer.aliyun.com/article/1510475

Omit(省略)

/**

Construct a type with the properties of T except for those in type K.

*/

type Omit = Pick>;

传入一个类型,和这个类型的几个属性,把传入的属性省略掉,组成一个新类型


使用举例

export interface Student {

name: string;

age: number;

class: string;

school: string;

}

export type PersonAttr = ‘name’ | ‘age’


export type StudentAttr = ‘name’ | ‘age’ | ‘class’ | ‘school’


const student1: Omit = {}


student1报错,提示没有属性’name’、‘age’

NonNullable(不能为null)

/**

Exclude null and undefined from T

*/

type NonNullable = T extends null | undefined ? never : T;

字面意思,不能为空

使用举例
export interface Student {

name: string;

age: number;

}

const student1: NonNullable = null


student1赋值为null会报错(在tsconfig.json配置文件中开启类型检查,“skipLibCheck”: false

Parameters(参数)

/**

Obtain the parameters of a function type in a tuple

*/

type Parameters any> = T extends (…args: infer P) => any ? P : never;

获取传入函数的参数组成的类型

使用举例

export interface Student {

name: string;

age: number;

}

export interface StudentFunc {

(name: string, age: number): Student

}

const student1: Parameters


student1的类型为[name: string, age: number]

ConstructorParameters(构造参数)

/**

Obtain the parameters of a constructor function type in a tuple

*/

type ConstructorParameters any> = T extends abstract new (…args: infer P) => any ? P : never;

获取传入构造函数的参数组成的类型


使用举例

export interface Student {

name: string;

age: number;

}

export interface StudentConstructor {

new (name: string, age: number): Student

}


const student1: ConstructorParameters


student1的类型为[name: string, age: number]

ReturnType(返回类型)

/**

Obtain the return type of a function type

*/

type ReturnType any> = T extends (…args: any) => infer R ? R : any;

获取传入函数的返回类型


使用举例

export interface Student {

name: string;

age: number;

}

export interface StudentFunc {

(name: string, age: number): Student

}

const student1: ReturnType = {}


student1的类型为Student

TypeScript内置类型一览(Record<string,any>等等)(下):https://developer.aliyun.com/article/1510478

相关文章
|
3天前
|
JavaScript
typeScript进阶(9)_type类型别名
本文介绍了TypeScript中类型别名的概念和用法。类型别名使用`type`关键字定义,可以为现有类型起一个新的名字,使代码更加清晰易懂。文章通过具体示例展示了如何定义类型别名以及如何在函数中使用类型别名。
15 1
typeScript进阶(9)_type类型别名
|
3天前
|
存储 JavaScript
typeScript进阶(11)_元组类型
本文介绍了TypeScript中的元组(Tuple)类型,它是一种特殊的数组类型,可以存储不同类型的元素。文章通过示例展示了如何声明元组类型以及如何给元组赋值。元组类型在定义时需要指定数组中每一项的类型,且在赋值时必须满足这些类型约束。此外,还探讨了如何给元组类型添加额外的元素,这些元素必须符合元组类型中定义的类型联合。
13 0
|
3天前
|
JavaScript
typeScript进阶(10)_字符串字面量类型
本文介绍了TypeScript中的字符串字面量类型,这种类型用来限制变量只能是某些特定的字符串字面量。通过使用`type`关键字声明,可以确保变量的值限定在预定义的字符串字面量集合中。文章通过示例代码展示了如何声明和使用字符串字面量类型,并说明了它在函数默认参数中的应用。
13 0
|
3天前
|
JavaScript 前端开发
typeScript基础(8)_ts类型断言
本文介绍了TypeScript中的类型断言,它用于在编译时告诉TypeScript某个对象具有特定的类型,即使它看起来不具备。类型断言可以用来访问一个类型上存在而另一个类型上不存在的属性或方法。需要注意的是,类型断言并不会在运行时改变JavaScript的行为,因此如果断言不当,运行时仍然可能出错。文章还提醒避免将类型断言为`any`类型或进行多重断言。
11 1
|
3天前
|
JavaScript
typeScript基础(6)_数组类型
本文介绍了TypeScript中数组的类型表示方法,包括直接使用类型加`[]`定义数组类型,以及使用数组泛型`Array<类型>`定义数组。同时,还展示了如何定义包含多种数据类型的数组。
16 1
|
1天前
|
JavaScript 前端开发 编译器
TypeScript,从0到入门带你进入类型的世界
该文章提供了TypeScript的入门指南,从安装配置到基础语法,再到高级特性如泛型、接口等的使用,帮助初学者快速掌握TypeScript的基本用法。
|
3天前
|
JavaScript
typeScript基础(7)_函数的类型
本文介绍了TypeScript中函数的类型,包括函数声明与函数表达式的类型注解,如何定义函数的参数类型、返回类型,以及可选参数和参数默认值。还探讨了函数的剩余参数、如何使用接口定义函数的形状,以及函数重载的概念和实践。
9 0
|
3天前
|
Java 索引
java基础(13)String类
本文介绍了Java中String类的多种操作方法,包括字符串拼接、获取长度、去除空格、替换、截取、分割、比较和查找字符等。
12 0
java基础(13)String类
|
1月前
|
API 索引
String类下常用API
String类下常用API
33 1
|
1月前
for循环和String类下方法的一个练习题
for循环和String类下方法的一个练习题
43 1