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

相关文章
|
20天前
|
JavaScript
typeScript进阶(9)_type类型别名
本文介绍了TypeScript中类型别名的概念和用法。类型别名使用`type`关键字定义,可以为现有类型起一个新的名字,使代码更加清晰易懂。文章通过具体示例展示了如何定义类型别名以及如何在函数中使用类型别名。
34 1
typeScript进阶(9)_type类型别名
|
20天前
|
存储 JavaScript
typeScript进阶(11)_元组类型
本文介绍了TypeScript中的元组(Tuple)类型,它是一种特殊的数组类型,可以存储不同类型的元素。文章通过示例展示了如何声明元组类型以及如何给元组赋值。元组类型在定义时需要指定数组中每一项的类型,且在赋值时必须满足这些类型约束。此外,还探讨了如何给元组类型添加额外的元素,这些元素必须符合元组类型中定义的类型联合。
32 0
|
20天前
|
JavaScript
typeScript进阶(10)_字符串字面量类型
本文介绍了TypeScript中的字符串字面量类型,这种类型用来限制变量只能是某些特定的字符串字面量。通过使用`type`关键字声明,可以确保变量的值限定在预定义的字符串字面量集合中。文章通过示例代码展示了如何声明和使用字符串字面量类型,并说明了它在函数默认参数中的应用。
28 0
|
1天前
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
12 3
|
4天前
|
JavaScript 前端开发 安全
使用 TypeScript 加强 React 组件的类型安全
【10月更文挑战第1天】使用 TypeScript 加强 React 组件的类型安全
16 3
|
20天前
|
JavaScript 前端开发
typeScript基础(8)_ts类型断言
本文介绍了TypeScript中的类型断言,它用于在编译时告诉TypeScript某个对象具有特定的类型,即使它看起来不具备。类型断言可以用来访问一个类型上存在而另一个类型上不存在的属性或方法。需要注意的是,类型断言并不会在运行时改变JavaScript的行为,因此如果断言不当,运行时仍然可能出错。文章还提醒避免将类型断言为`any`类型或进行多重断言。
19 1
|
18天前
|
JavaScript 前端开发 编译器
TypeScript,从0到入门带你进入类型的世界
该文章提供了TypeScript的入门指南,从安装配置到基础语法,再到高级特性如泛型、接口等的使用,帮助初学者快速掌握TypeScript的基本用法。
|
20天前
|
Java 索引
java基础(13)String类
本文介绍了Java中String类的多种操作方法,包括字符串拼接、获取长度、去除空格、替换、截取、分割、比较和查找字符等。
29 0
java基础(13)String类
|
17天前
|
安全 Java
String类-知识回顾①
这篇文章回顾了Java中String类的相关知识点,包括`==`操作符和`equals()`方法的区别、String类对象的不可变性及其好处、String常量池的概念,以及String对象的加法操作。文章通过代码示例详细解释了这些概念,并探讨了使用String常量池时的一些行为。
String类-知识回顾①
|
22小时前
|
存储 安全 Java
【一步一步了解Java系列】:认识String类
【一步一步了解Java系列】:认识String类
15 2