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 条件类型:超越简单类型检查
揭秘 TypeScript 条件类型:超越简单类型检查
|
3月前
|
JavaScript 安全 索引
TypeScript 高级类型工具:Partial, Required, Record 的妙用与陷阱
TypeScript 高级类型工具:Partial, Required, Record 的妙用与陷阱
|
3月前
|
JavaScript 安全 IDE
TypeScript 类型体操:别让 `any` 毁了你的安全网!
TypeScript 类型体操:别让 `any` 毁了你的安全网!
|
27天前
|
数据安全/隐私保护
【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,以禁用明文输出。
|
3月前
|
JavaScript 安全 编译器
TypeScript 类型守卫:让你的类型系统更智能
TypeScript 类型守卫:让你的类型系统更智能
|
4月前
|
存储 JSON JavaScript
[go]byte类型, string 类型, json 类型
本文介绍了Go语言中byte类型的基本概念、特点及用法。byte是8位无符号整数,取值范围为0-255,常用于二进制数据操作,如网络通信和文件读写。文章还详细说明了byte与字符串的转换、遍历byte数据以及与其他类型间的转换。此外,探讨了Go中json.Marshal和json.Unmarshal函数实现[]byte与JSON间的转换,并对比了[]byte与JSON的区别,帮助开发者更好地理解其应用场景与差异。
153 2
|
8月前
|
存储 安全 JavaScript
TypeScript-内置应用程序类型-Recode
通过使用 `Record` 类型,开发者可以显著提升代码的安全性和可维护性。无论是配置对象、字典结构还是动态表单,`Record` 类型都提供了一个简洁、类型安全的解决方案。
358 82
|
11天前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
123 5
|
4月前
|
存储 编译器 C语言
关于string的‘\0‘与string,vector构造特点,反迭代器与迭代器类等的讨论
你真的了解string的'\0'么?你知道创建一个string a("abcddddddddddddddddddddddddd", 16);这样的string对象要创建多少个对象么?你知道string与vector进行扩容时进行了怎么的操作么?你知道怎么求Vector 最大 最小值 索引 位置么?
87 0
|
7月前
|
缓存 安全 Java
《从头开始学java,一天一个知识点》之:字符串处理:String类的核心API
🌱 **《字符串处理:String类的核心API》一分钟速通!** 本文快速介绍Java中String类的3个高频API:`substring`、`indexOf`和`split`,并通过代码示例展示其用法。重点提示:`substring`的结束索引不包含该位置,`split`支持正则表达式。进一步探讨了String不可变性的高效设计原理及企业级编码规范,如避免使用`new String()`、拼接时使用`StringBuilder`等。最后通过互动解密游戏帮助读者巩固知识。 (上一篇:《多维数组与常见操作》 | 下一篇预告:《输入与输出:Scanner与System类》)
159 11

热门文章

最新文章