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> = ‘’



相关文章
|
10天前
|
前端开发 JavaScript 安全
TypeScript在React Hooks中的应用:提升React开发的类型安全与可维护性
【7月更文挑战第17天】TypeScript在React Hooks中的应用极大地提升了React应用的类型安全性和可维护性。通过为状态、依赖项和自定义Hooks指定明确的类型,开发者可以编写更加健壮、易于理解和维护的代码。随着React和TypeScript的不断发展,结合两者的优势将成为构建现代Web应用的标准做法。
|
9天前
|
存储 SQL 分布式计算
MaxCompute产品使用合集之表中的某个列设置为string类型,并且超过了8M,该如何处理
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
17天前
|
JavaScript 开发者 索引
TypeScript接口与类型别名:深入解析与应用实践
【7月更文挑战第10天】TypeScript的接口和类型别名是定义类型的关键工具。接口描述对象结构,用于类、对象和函数参数的形状约束,支持可选、只读属性及继承。类型别名则为复杂类型提供新名称,便于重用和简化。接口适合面向对象场景,类型别名在类型重用和复杂类型简化时更有优势。选择时要考虑场景和灵活性。
|
21天前
|
JavaScript 前端开发 程序员
Typescript 【实用教程】(2024最新版)含类型声明,类型断言,函数,接口,泛型等
Typescript 【实用教程】(2024最新版)含类型声明,类型断言,函数,接口,泛型等
17 0
|
22天前
|
存储 NoSQL Redis
Redis07命令-String类型字符串,不管是哪种格式,底层都是字节数组形式存储的,最大空间不超过512m,SET添加,MSET批量添加,INCRBY age 2可以,MSET,INCRSETEX
Redis07命令-String类型字符串,不管是哪种格式,底层都是字节数组形式存储的,最大空间不超过512m,SET添加,MSET批量添加,INCRBY age 2可以,MSET,INCRSETEX
TS定义布尔值,let flag:boolean = true,定义数字类型 let a1:number = 10,赋值 let str1:string = ‘‘,打印c~.log($(str1))
TS定义布尔值,let flag:boolean = true,定义数字类型 let a1:number = 10,赋值 let str1:string = ‘‘,打印c~.log($(str1))
TS,数据类型概述,常见的基本数据类型有number/string/boolean/undefined/null,字符串用““,let food: string = ‘糖葫芦‘,布尔类型
TS,数据类型概述,常见的基本数据类型有number/string/boolean/undefined/null,字符串用““,let food: string = ‘糖葫芦‘,布尔类型
|
25天前
|
JavaScript 安全
TypeScript(十一)泛型工具类型
TypeScript(十一)泛型工具类型
19 0
|
25天前
|
JavaScript 前端开发 编译器
TypeScript(五)类型别名及类型符号
TypeScript(五)类型别名及类型符号
22 0
|
25天前
|
JavaScript 前端开发
TypeScript(二)基本类型和特殊类型
TypeScript(二)基本类型和特殊类型
14 0