当前位置:网站首页>Generic types in classes and generic types
Generic types in classes and generic types
2022-04-22 05:06:00 【Xiao Er Li】
This article will introduce TypeScript Generic types of classes and related knowledge of generic types in .
Generics in classes
Define class generics , Constructor receives a generic array , Specify the specific type when creating class instances , And pass the specified type array .
class DataManager<T> {
// Constructor passes a generic array
constructor(private params: T[]) { }
getItem(index: number): T {
return this.params[index]
}
}
// Pass an array of types represented by generics
const data = new DataManager<number>([1, 3, 4])
console.log(data.getItem(0))
console.log(data.getItem(1))
console.log(data.getItem(2))

The generic type
Use interface Define a class , And specify the class as the type of class generic (extends), When creating an instance , You need to pass the attribute value contained in the generic type .
interface Item {
name:string
}
class DataManager<T extends Item> {
constructor(private params: T[]) { }
getItem(index: number): string {
return this.params[index].name
}
}
// The object passed must contain Item Class contains properties
const data = new DataManager<Item>([
{
name:'jake'
},
{
name:'smith'
}
])
console.log(data.getItem(0))
console.log(data.getItem(1))


Union type generic parameter
Use the union type to specify the data type received by the generic type .
// Use a union type to specify that a generic type can receive number and string Type parameter
class DataManager<T extends number|string> {
constructor(private params: T[]) { }
getItem(index: number): T {
return this.params[index]
}
}
const data = new DataManager<string>(['jake','smith'])
console.log(data.getItem(0))
console.log(data.getItem(1))

Use a generic annotation as a concrete type

版权声明
本文为[Xiao Er Li]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210650388071.html
边栏推荐
- ObjectBox使用记录
- MySQL forget root password solution
- Esp32 vhci realizes ble broadcasting, which is so magical
- PHP quickly raises a value in a two-dimensional array to an array key value, array_ The wonderful use of column
- bugly使用记录
- [chestnut sugar GIS] ArcMap - Model Builder - batch clipping grid data
- VIM is so difficult to use, why are so many people keen?
- The problem that the video in video becomes muted when the mouse scrolls
- 常见的测试方式
- 什么是幂等性
猜你喜欢

Pydeck enables efficient visual rendering of millions of data points

One is based on Net core3 1's open source project helps you thoroughly understand the WPF framework prism

Morphological operation of OpenCV image processing

JVM - G1 garbage collector

Sharing of enterprise data leakage prevention scheme
![[I. XXX pest detection project] 2. Trying to improve the network structure: resnet50, Se, CBAM, feature fusion](/img/78/c50eaa02e26ef7c5e88ec8ad92ab13.png)
[I. XXX pest detection project] 2. Trying to improve the network structure: resnet50, Se, CBAM, feature fusion

Chapter V function

EMO-DB 數據集的 Speech 特征提取
![[chestnut sugar GIS] SuperMap - how to create hyperlinks for data](/img/6d/8b46c96d3ec1005481919e5e81ce80.png)
[chestnut sugar GIS] SuperMap - how to create hyperlinks for data

Swagger UI简介
随机推荐
Introduction to swagger UI
First knowledge of UI automation (inspect.exe + uiautomation)
The problem that the video in video becomes muted when the mouse scrolls
Learn from me: how to release your own plugin or package in China
JVM——》CMS
Autojs automation script how to develop on the computer, detailed reliable tutorial!!!
The urlcode of PHP cannot restore the original URL
Spark 入门程序 : WordCount
Small bug -- hidden problem of Guide Package
JUnit common notes
[I. XXX pest detection project] 2. Trying to improve the network structure: resnet50, Se, CBAM, feature fusion
Nexus私服——(二) 3.2.0版 控制台安装,初始密码位置
[I. XXX pest detection items] 3. Loss function attempt: focal loss
JUnit assertion
Image segmentation using deep learning: a survey
QBoxSet、QBoxPlotSeries
ObjectBox使用记录
Opencv usage record of neural network learning
cv2. drawContours()、cv2. findContours()、cv2. Usage analysis of boundingrect (IMG) function
TDD开发模式与DDD开发模式