当前位置:网站首页>Generics and generic constraints of typescript
Generics and generic constraints of typescript
2022-07-02 05:51:00 【User experience officer Dalong】
One 、 Generic
In the use of ts When encapsulating a function , Because the input parameter is not fixed , The inevitable parameter type cannot be fixed , At this point, you need to use generics to define parameter types . Generics can be understood as broad types , Usually used for classes and functions .
example 1. Class
class Person<T>{
private name: T;
constructor(value: T) {
this.name = value;
}
}
const p = new Person<string>('Dalon')example 2. Use generics in functions
function identity<T>(data: T): T {
console.log(data)
return data;
}
const id1 = identity<string>('hey boy!');
const id2 = identity<number>(123456);example 3. Use generics in interfaces
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'
}example 3 Medium T1 and T2 Generics add default types , Similar to the default parameters of a function .
Two 、 Generic constraint
Generic constraint is to control the type of generic , If it is limited to object Type or specify interface type . When using properties or methods of generic parameters in functions , You need to constrain generics .
example 1. Through interface constraints
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // without Lengthwise Generic constraint , Will report a mistake Error: T doesn't have .length
return arg;
}example 2. object constraint
function getKeys<T extends object>(data:T) :string[] {
const keys:Array<string> = Object.keys(data)
return keys
}Above example 2 Of getKeys Method to get all attribute names of an object , because Object.keys yes object Prototype method of , So ask for data It has to be a object type .
( End )
reference :
Generic · TypeScript Chinese net · TypeScript——JavaScript Superset
边栏推荐
- Get the details of the next largest number
- php获取cpu使用率、硬盘使用、内存使用
- Zzuli:1061 sequential output of digits
- Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
- Huawei Hongmeng OS, is it OK?
- 460. LFU cache bidirectional linked list
- DRM display framework as I understand it
- Zzuli: maximum Convention and minimum common multiple
- [Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University
- 线程池概述
猜你喜欢

Matplotlib double Y axis + adjust legend position

VSCode paste image插件保存图片路径设置

《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记

2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions

RGB 无限立方体(高级版)

Opencv LBP features

Can't the dist packaged by vite be opened directly in the browser

2022-2-15 learning xiangniuke project - Section 8 check login status

idea开发工具常用的插件合集汇总

vite如何兼容低版本浏览器
随机推荐
Matplotlib double Y axis + adjust legend position
Zzuli:1061 sequential output of digits
【技术随记-08】
Determine whether there is an element in the string type
Opencv LBP features
Basic use of form
Zzuli:1068 binary number
深度学习分类网络--VGGNet
死磕大屏UI,FineReport开发日记
Test case
PHP inner class name is the same as the inner class method name
How to change the IP address of computer mobile phone simulator
1035 Password
Spark概述
php内类名称与类内方法名相同
c语言中的几个关键字
Zzuli:1065 count the number of numeric characters
数理统计与机器学习
Generate QR code
数据库学习总结5