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

相关文章
|
5月前
|
JavaScript 前端开发
揭秘 TypeScript 条件类型:超越简单类型检查
揭秘 TypeScript 条件类型:超越简单类型检查
|
5月前
|
JavaScript 安全 索引
TypeScript 高级类型工具:Partial, Required, Record 的妙用与陷阱
TypeScript 高级类型工具:Partial, Required, Record 的妙用与陷阱
|
5月前
|
JavaScript 安全 IDE
TypeScript 类型体操:别让 `any` 毁了你的安全网!
TypeScript 类型体操:别让 `any` 毁了你的安全网!
|
3月前
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
147 0
|
6月前
|
存储 JSON JavaScript
[go]byte类型, string 类型, json 类型
本文介绍了Go语言中byte类型的基本概念、特点及用法。byte是8位无符号整数,取值范围为0-255,常用于二进制数据操作,如网络通信和文件读写。文章还详细说明了byte与字符串的转换、遍历byte数据以及与其他类型间的转换。此外,探讨了Go中json.Marshal和json.Unmarshal函数实现[]byte与JSON间的转换,并对比了[]byte与JSON的区别,帮助开发者更好地理解其应用场景与差异。
266 2
|
5月前
|
JavaScript 安全 编译器
TypeScript 类型守卫:让你的类型系统更智能
TypeScript 类型守卫:让你的类型系统更智能
|
10月前
|
存储 安全 JavaScript
TypeScript-内置应用程序类型-Recode
通过使用 `Record` 类型,开发者可以显著提升代码的安全性和可维护性。无论是配置对象、字典结构还是动态表单,`Record` 类型都提供了一个简洁、类型安全的解决方案。
477 82
|
JavaScript 前端开发 安全
深入理解TypeScript:增强JavaScript的类型安全性
【10月更文挑战第8天】深入理解TypeScript:增强JavaScript的类型安全性
330 0
|
JavaScript 前端开发 开发者
深入理解TypeScript:类型系统与实用技巧
【10月更文挑战第8天】深入理解TypeScript:类型系统与实用技巧
|
JavaScript 前端开发 安全
使用 TypeScript 加强 React 组件的类型安全
【10月更文挑战第1天】使用 TypeScript 加强 React 组件的类型安全
325 106