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

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

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


InstanceType(构造返回类型、实例类型)

/**


Obtain the return type of a constructor function type

*/

type InstanceType<T extends abstract new (…args: any) => any> = T extends abstract new (…args: any) => infer R ? R : any;

获取传入构造函数的返回类型


使用举例

const Student = class {

name: string;

age: number;

constructor (name: string, age: number) {

this.name = name

this.age = age

}

showInfo () {

console.log('name: ', this.name, 'age: ', this.age);

}

}

const student1: InstanceType<typeof Student> = new Student(‘张三’, 20)


个人认为这是一个非常好用的内置类型,目前在前端项目中,class是用的越来越多了,在TS中,class其实也是可以用作类型声明空间的,用来描述对象类型,但是一般来说好像很少这样用的,一般用interface或者type居多

export class Student {

name: string;

age: number;

}

所以一般就是直接把class用作变量声明空间,但是对于 class new 出的实例,怎么描述它的类型呢,就如上文的,直接const student1: Student那是铁定会报错的,因为Student用作变量声明空间,没有用作类型声明空间(听起来好绕),这时候就可以用到InstanceType,完美解决问题

Uppercase(大写)

/**

Convert string literal type to uppercase

*/

type Uppercase<S extends string> = intrinsic;

变大写

使用举例
export type StudentSexType = ‘male’ | ‘female’

const studentSex: Uppercase<StudentSexType> = ‘MALE’



Lowercase(小写)

/**

Convert string literal type to lowercase

*/

type Lowercase<S extends string> = intrinsic;

变小写

使用举例
export type StudentSexType = ‘MALE’ | ‘FEMALE’

const studentSex: Lowercase<StudentSexType> = ‘’



Capitalize(首字母大写)

/**

Convert first character of string literal type to uppercase

*/

type Capitalize<S extends string> = intrinsic;

首字母变大写

使用举例
export type StudentSexType = ‘male’ | ‘female’

const studentSex: Capitalize<StudentSexType> = ‘’



Uncapitalize(首字母小写)

/**

Convert first character of string literal type to lowercase

*/

type Uncapitalize<S extends string> = intrinsic;

首字母变小写

使用举例
export type StudentSexType = ‘MALE’ | ‘FEMALE’

const studentSex: Uncapitalize<StudentSexType> = ‘’



相关文章
|
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,以禁用明文输出。
144 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
|
2月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
326 5
|
6月前
|
存储 编译器 C语言
关于string的‘\0‘与string,vector构造特点,反迭代器与迭代器类等的讨论
你真的了解string的'\0'么?你知道创建一个string a("abcddddddddddddddddddddddddd", 16);这样的string对象要创建多少个对象么?你知道string与vector进行扩容时进行了怎么的操作么?你知道怎么求Vector 最大 最小值 索引 位置么?
181 0
|
9月前
|
缓存 安全 Java
《从头开始学java,一天一个知识点》之:字符串处理:String类的核心API
🌱 **《字符串处理:String类的核心API》一分钟速通!** 本文快速介绍Java中String类的3个高频API:`substring`、`indexOf`和`split`,并通过代码示例展示其用法。重点提示:`substring`的结束索引不包含该位置,`split`支持正则表达式。进一步探讨了String不可变性的高效设计原理及企业级编码规范,如避免使用`new String()`、拼接时使用`StringBuilder`等。最后通过互动解密游戏帮助读者巩固知识。 (上一篇:《多维数组与常见操作》 | 下一篇预告:《输入与输出:Scanner与System类》)
282 11