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天前
|
SQL 分布式计算 DataWorks
DataWorks产品使用合集之如何将STRING类型转换为DATETIME类型
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
26天前
|
JavaScript
TypeScript——不能将类型“HTMLElement | null”分配给类型“HTMLElement”
TypeScript——不能将类型“HTMLElement | null”分配给类型“HTMLElement”
26 4
|
7天前
|
JavaScript 前端开发 编译器
Angular 与 TypeScript 强强联手太厉害啦!强类型编程带来巨大开发优势,快来一探究竟!
【8月更文挑战第31天】作为一名前端开发者,我致力于探索各种技术框架以提升开发效率与代码质量。近期深入研究了Angular与TypeScript的结合,体验到强类型编程带来的显著优势。Angular是一款强大的前端框架,而TypeScript则是由微软开发的一种强类型语言,为JavaScript增添了静态类型检查等功能。
14 0
|
12天前
|
存储 NoSQL 索引
MPP架构数据仓库使用问题之在ORC文件中,String类型字段是怎么进行编码的
MPP架构数据仓库使用问题之在ORC文件中,String类型字段是怎么进行编码的
|
14天前
|
开发工具 数据安全/隐私保护
【Azure Developer】使用MSAL4J 与 ADAL4J 的SDK时候,遇见了类型冲突问题 "java.util.Collections$SingletonList cannot be cast to java.lang.String"
【Azure Developer】使用MSAL4J 与 ADAL4J 的SDK时候,遇见了类型冲突问题 "java.util.Collections$SingletonList cannot be cast to java.lang.String"
|
17天前
|
JavaScript 前端开发 安全
TypeScript:解锁JavaScript的超级英雄模式!类型系统如何化身守护神,拯救你的代码免于崩溃与混乱,戏剧性变革开发体验!
【8月更文挑战第22天】TypeScript作为JavaScript的超集,引入了强大的类型系统,提升了编程的安全性和效率。本文通过案例展示TypeScript如何增强JavaScript:1) 显式类型声明确保函数参数与返回值的准确性;2) 接口和类加强类型检查,保证对象结构符合预期;3) 泛型编程提高代码复用性和灵活性。这些特性共同推动了前端开发的标准化和规模化。
35 0
|
17天前
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
29 0
|
25天前
|
JavaScript
TypeScript——Record类型
TypeScript——Record类型
30 0
|
2月前
|
前端开发 JavaScript 安全
TypeScript在React Hooks中的应用:提升React开发的类型安全与可维护性
【7月更文挑战第17天】TypeScript在React Hooks中的应用极大地提升了React应用的类型安全性和可维护性。通过为状态、依赖项和自定义Hooks指定明确的类型,开发者可以编写更加健壮、易于理解和维护的代码。随着React和TypeScript的不断发展,结合两者的优势将成为构建现代Web应用的标准做法。
|
1月前
|
JavaScript 编译器
typescript 解决变量多类型访问属性报错--工作随记
typescript 解决变量多类型访问属性报错--工作随记