当前位置:网站首页>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
边栏推荐
- 金融门户相关信息
- php父类(parent)
- Fundamentals of software testing
- Importation de studio visuel
- Practice C language advanced address book design
- RNN recurrent neural network
- PHP inner class name is the same as the inner class method name
- [golang syntax] be careful with the copy of slices
- Zzuli:1061 sequential output of digits
- Pytorch Chinese document
猜你喜欢

Gcnet: non - local Networks meet Squeeze excitation Networks and Beyond

Reading notes of cgnf: conditional graph neural fields

Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan

文件包含漏洞(二)

Opencv LBP features

“簡單”的無限魔方

3D 打印机 G 代码命令:完整列表和教程

DRM display framework as I understand it

File contains vulnerability (I)

CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
随机推荐
h5跳小程序
深度学习分类网络--VGGNet
TypeScript的泛型和泛型约束
1036 Boys vs Girls
Can't the dist packaged by vite be opened directly in the browser
深度学习分类网络--Network in Network
线程池概述
"Original, excellent and vulgar" in operation and maintenance work
ThreadLocal memory leak
GRBL 软件:简单解释的基础知识
460. LFU cache bidirectional linked list
Addchild() and addattribute() functions in PHP
How to change the IP address of computer mobile phone simulator
Go language web development is very simple: use templates to separate views from logic
PHP read file (read the specified line containing a string in the file)
Zzuli: maximum Convention and minimum common multiple
php内类名称与类内方法名相同
Alibaba: open source and self-developed liquid cooling data center technology
php读文件(读取文件内含有某字符串的指定行)
Balsamiq wireframes free installation