TypeScript 如何开发泛型类
在开发实际的泛型类之前,让我们先了解泛型类。 TypeScript中的泛型类可以处理多种类型的数据。它是一种参数的种类,并使用尖括号(<>)来表示。这代表了类将使用的数据类型。然后,类型参数可以在类的属性和函数中使用,使得类能够与其他数据类型灵活且可重用。
我们来简要说明一下。假设在一个示例中,类型参数被表示为”T”,简单的泛型类”MyGenericClass”的属性”value”也可以被创建。如果这个类用不同的类型实例化,比如”number”或”string”,那么”value”属性将具有相应的类型。
由于同一个类可以与多种数据类型一起使用,这给你的代码提供了额外的灵活性和重用性。你还可以使用约束来限制用作类型参数的种类。
语法
TypeScript中创建泛型类的语法如下所示 –
class ClassName<TypeParameter> {
// class properties and methods
}
其中”ClassName”是类的名称,”TypeParameter”是类将处理的数据类型的占位符。
示例1
这个示例演示了在TypeScript中使用泛型类来创建一个可以处理多种数据类型的类。该类使用一个类型参数T来定义,在类的属性和方法中使用,使得该类可以灵活且可重用地处理不同类型的数据。类”Queue”有一个名为”data”的私有属性,其类型为T的数组。
该类还有三个方法:”enqueue”将一个项添加到队列的末尾,”dequeue”从队列的前端移除一个项,”peek”返回队列前端的项,但不将其移除。我们创建了两个Queue类的实例,一个用于处理数字,另一个用于处理字符串。该类可以处理不同的数据类型,使我们的代码更加灵活和可重用。
class Queue<T> {
private data: T[] = []
// add an item to the end of the queue
enqueue(item: T) {
this.data.push(item)
}
// remove an item from the front of the queue
dequeue(): T | undefined {
return this.data.shift()
}
// get the item at the front of the queue
peek(): T | undefined {
return this.data[0]
}
}
let numberQueue = new Queue<number>()
numberQueue.enqueue(1)
numberQueue.enqueue(2)
console.log(numberQueue.peek())
console.log(numberQueue.dequeue())
console.log(numberQueue.peek())
let stringQueue = new Queue<string>()
stringQueue.enqueue('Hello')
stringQueue.enqueue('world')
console.log(stringQueue.peek())
console.log(stringQueue.dequeue())
console.log(stringQueue.peek())
在编译时,它将生成以下JavaScript代码。
var Queue = /** @class */ (function () {
function Queue() {
this.data = [];
}
// add an item to the end of the queue
Queue.prototype.enqueue = function (item) {
this.data.push(item);
};
// remove an item from the front of the queue
Queue.prototype.dequeue = function () {
return this.data.shift();
};
// get the item at the front of the queue
Queue.prototype.peek = function () {
return this.data[0];
};
return Queue;
}());
var numberQueue = new Queue();
numberQueue.enqueue(1);
numberQueue.enqueue(2);
console.log(numberQueue.peek());
console.log(numberQueue.dequeue());
console.log(numberQueue.peek());
var stringQueue = new Queue();
stringQueue.enqueue('Hello');
stringQueue.enqueue('world');
console.log(stringQueue.peek());
console.log(stringQueue.dequeue());
console.log(stringQueue.peek());
输出
上面的代码将产生以下输出:
1
1
2
Hello
Hello
world
示例2
在这个示例中,我们将开发另一个具有两种泛型属性的通用类。”KeyValuePair”类具有两个私有属性,”key”和”value”,分别为类型T和U。该类还有两个方法,”getKey”和”getValue”,分别返回key和value属性。
该类可以用数字、字符串或对象等数据类型实例化,用于key和value。我们创建了两个KeyValuePair类的实例,一个key为字符串,value为数字,另一个key为字符串,value为对象。该类可以处理key和value的不同数据类型,使我们的代码更加灵活和可重用。
class KeyValuePair<T, U> {
private key: T
private value: U
constructor(key: T, value: U) {
this.key = key
this.value = value
}
// method to get the key
getKey(): T {
return this.key
}
// method to get the value
getValue(): U {
return this.value
}
}
let numberKeyValuePair = new KeyValuePair<string, number>('age', 20)
console.log(numberKeyValuePair.getKey()) // "age"
console.log(numberKeyValuePair.getValue()) // 20
let objectKeyValuePair = new KeyValuePair<string, object>('person', {
name: 'Tutorialspoint',
age: 10,
})
console.log(objectKeyValuePair.getKey()) // "person"
console.log(objectKeyValuePair.getValue()) // {name: "Tutorialspoint", age: 10}
在编译时,它将生成以下的JavaScript代码。
var KeyValuePair = /** @class */ (function () {
function KeyValuePair(key, value) {
this.key = key;
this.value = value;
}
// method to get the key
KeyValuePair.prototype.getKey = function () {
return this.key;
};
// method to get the value
KeyValuePair.prototype.getValue = function () {
return this.value;
};
return KeyValuePair;
}());
var numberKeyValuePair = new KeyValuePair('age', 20);
console.log(numberKeyValuePair.getKey()); // "age"
console.log(numberKeyValuePair.getValue()); // 20
var objectKeyValuePair = new KeyValuePair('person', {
name: 'Tutorialspoint',
age: 10
});
console.log(objectKeyValuePair.getKey()); // "person"
console.log(objectKeyValuePair.getValue()); // {name: "Tutorialspoint", age: 10}
输出
上述代码将产生以下输出 –
age
20
person
{ name: 'Tutorialspoint', age: 10 }
在TypeScript中使用通用类可以带来更灵活、可重用和可维护的代码。此外,TypeScript的类型检查系统确保通用类中使用的类型在编译时被正确使用,进一步提高了代码的整体质量和安全性。通用类是TypeScript的一个强大功能,可用于编写更可靠和可重用的代码。