兼收并蓄 TypeScript - 类: generics
兼收并蓄 TypeScript - 类: generics
示例如下:
class\generics.ts
{
// Generics - 泛型
// 泛型的简单示例
function createArray<T>(length: number, value: T): Array<T> {
let result: T[] = [];
for (let i = 0; i < length; i++) {
result[i] = value;
}
return result;
}
let a = createArray<string>(3, 'x');
console.log(a); // ['x', 'x', 'x']
// 可以省略 <string> 因为 TypeScript 可以根据传入的参数推导出类型
let b = createArray(3, 'x');
console.log(b); // ['x', 'x', 'x']
}
{
// 可以定义多个不同的泛型类型
function swap<T, U>(tuple: [T, U]): [U, T] {
return [tuple[1], tuple[0]];
}
let a = swap<number, string>([7, 'seven']);
console.log(a); // ['seven', 7]
// 可以省略 <number, string> 因为 TypeScript 可以根据传入的参数推导出类型
let b = swap([7, 'seven']);
console.log(b); // ['seven', 7]
}
{
// 泛型约束,用于约束泛型必须继承自某个类或某些接口
interface Interface1 {
name: string;
}
interface Interface2 {
age: number;
}
class Class1 implements Interface1, Interface2 {
constructor(public name: string, public age: number) {
}
}
// 泛型 T 必须继承自 Interface1 和 Interface2
function hello<T extends Interface1 & Interface2>(p: T): string {
return `${p.name} ${p.age}`;
}
let a = new Class1('webabcd', 44);
console.log(hello(a)); // webabcd 44
}
{
// 通过接口定义泛型
interface CreateArray<T> {
(length: number, value: T): Array<T>;
}
let createArray: CreateArray<string> = function<T>(length: number, value: T): Array<T> {
let result: T[] = [];
for (let i = 0; i < length; i++) {
result[i] = value;
}
return result;
}
let a = createArray(3, 'x');
console.log(a); // ['x', 'x', 'x']
}
{
// 泛型类
class Class1<T> {
salt?: T;
add?: (x: T, y: T) => T;
hello(x: T):string {
return `hello:${x}`;
}
}
let a = new Class1<number>();
a.salt = 10;
a.add = function(x, y) {
return (x + y) * this.salt!;
};
console.log(a.add(2, 3), a.hello(100)); // 50 hello:100
}
{
// 泛型接口
interface MyInterface<T, U> {
hello(input: T): U;
}
// 实现泛型接口
class MyClass implements MyInterface<number, string> {
hello(input: number): string {
return `hello:${input}`;
}
}
let a = new MyClass();
console.log(a.hello(100)); // hello:100
}