当前位置:网站首页>TypeScript的泛型和泛型约束
TypeScript的泛型和泛型约束
2022-07-02 05:41:00 【用户体验官大龙】
一、泛型
在使用ts封装一个函数时,由于入参是不固定的,必然参数类型也无法固定,这时就要用到泛型来定义参数类型。泛型可以理解为宽泛的类型,通常用于类和函数。
例1. 类中使用泛型
class Person<T>{
private name: T;
constructor(value: T) {
this.name = value;
}
}
const p = new Person<string>('Dalon')例2. 函数中使用泛型
function identity<T>(data: T): T {
console.log(data)
return data;
}
const id1 = identity<string>('hey boy!');
const id2 = identity<number>(123456);例3. 接口中使用泛型
interface Person<T1=string, T2=number> {
name: T1
age: T2
}
const dalon:Person = {
name: 'dalon',
age: 18
}
const tom:Person<string, string> = {
name: 'Tom',
age: '18'
}例3中的T1和T2泛型添加了默认类型,类似于函数的默认参数。
二、泛型约束
泛型约束即是对泛型的类型进行约束控制,如限制为object类型或指定接口类型。当在函数里使用泛型参数的属性或者方法时,就需要对泛型进行约束。
例1. 通过接口约束
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // 如果没有Lengthwise泛型约束,会报错Error: T doesn't have .length
return arg;
}例2. object约束
function getKeys<T extends object>(data:T) :string[] {
const keys:Array<string> = Object.keys(data)
return keys
}上面例2的getKeys方法获取一个对象的所有属性名, 由于Object.keys是object的原型方法,所以要求data也必须是一个object类型。
(完)
参考文献:
边栏推荐
- 【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
- Zzuli:1065 count the number of numeric characters
- Simply encapsulate JS and apply it
- idea開發工具常用的插件合集匯總
- Gee series: Unit 1 Introduction to Google Earth engine
- 7.TCP的十一种状态集
- Zzuli:1062 greatest common divisor
- Php/js cookie sharing across domains
- Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
- h5跳小程序
猜你喜欢
![Gee series: unit 8 time series analysis in Google Earth engine [time series]](/img/a6/648ff959af93c22dc8605215a90535.jpg)
Gee series: unit 8 time series analysis in Google Earth engine [time series]

Huawei Hongmeng OS, is it OK?

Brew install * failed, solution

Online music player app

Record sentry's path of stepping on the pit

Visual studio import

all3dp. All Arduino projects in com website (2022.7.1)

KMP idea and template code

Ls1046nfs mount file system

Fabric. JS iText set italics manually
随机推荐
Zzuli:1068 binary number
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Pytorch Basics
数据的储存
RGB infinite cube (advanced version)
Ls1046nfs mount file system
centos8安装mysql8.0.22教程
Cube magique infini "simple"
Visual Studio導入
测试 - 用例篇
青训营--数据库实操项目
KMP idea and template code
Storage of data
“简单”的无限魔方
Software testing learning - day 4
Uva548 tree
Gee: remote sensing image composite and mosaic
Zzuli:1060 numbers in reverse order
Visual Studio导入
Disable access to external entities in XML parsing