TypeScript Generics All In one
TypeScript Generics All In one
TypeScript 泛型
- 代码逻辑复用
- 扩展性
- 设计模式
方法覆写, 直接覆盖
方法重载,参数个数或参数类型不同
test
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-12-07
* @modified
*
* @description TypeScript 泛型
* @augments
* @example
* @link
*
*/
const log = console.log;
// function addGenerator<T> (type: string) {
// // 方法覆写, 直接覆盖
// // 方法重载,参数个数或参数类型不同
// function add<T> (arg1: T, arg2: T): T {
// return arg1 + arg2;
// }
// return add;
// }
function addNumber (arg1: number, arg2: number): number {
return arg1 + arg2;
}
addNumber(1, 2);
// addNumber(1, `2`);
function addString (arg1: string, arg2: string): string {
return arg1 + arg2;
}
addString(`1`, `2`);
// addString(`1`, 2);
// function addGenerics<T>(arg1: T, arg2: T): T {
// // error TS2365: Operator '+' cannot be applied to types 'T' and 'T'.
// return arg1 + arg2;
// }
function add<T> (arg1: T, arg2: T): T {
return arg1 + arg2;
}
// 1. 泛型方法
let addGenericsMethod: <T>(arg1: T, arg2: T) => T = add;
// let addGenericsMethod: <T>(arg1: T, arg2: T) => T;
// addGenericsMethod = add;
addGenericsMethod(1, 2);
addGenericsMethod(`1`, `2`);
// 2. 泛型对象
let addGenericsObject: {
<T>(arg1: T, arg2: T): T
} = add;
// let addGenericsObject: {
// <T>(arg1: T, arg2: T): T
// };
// addGenericsObject = add;
addGenericsObject(1, 2);
addGenericsObject(`1`, `2`);
// 3. 泛型对象接口 一
interface addGenericsInterface {
<T>(arg1: T, arg2: T): T,
}
let addGenerics: addGenericsInterface = add;
// let addGenerics: addGenericsInterface;
// addGenerics = add;
addGenerics<number>(1, 2);
addGenerics<string>(`1`, `2`);
// 或
// addGenerics(1, 2);
// addGenerics(`1`, `2`);
// 3. 泛型对象接口 二
interface addGenericsInterface2<T> {
(arg1: T, arg2: T): T,
}
let addGenericsNumber: addGenericsInterface2<number> = add;
let addGenericsString: addGenericsInterface2<string> = add;
addGenericsNumber(1, 2);
addGenericsString(`1`, `2`);
// 4. 泛型 class 一
class addGenericsClass {
// Property 'add' has no initializer and is not definitely assigned in the constructor.ts(2564)
add: <T>(arg1: T, arg2: T) => T;
}
let addInstance = new addGenericsClass();
addInstance.add = add;
addInstance.add<number>(1, 2);
addInstance.add<string>(`1`, `2`);
// 或
// addInstance.add(1, 2);
// addInstance.add(`1`, `2`);
// 4. 泛型 class 二
class addGenericsClass2<T> {
// Property 'add' has no initializer and is not definitely assigned in the constructor.ts(2564)
add: (arg1: T, arg2: T) => T;
}
// A 'new' expression with type arguments must always be followed by a parenthesized argument list.ts(1384)
// let addInstanceNumber = new addGenericsClass2<number>;
// addInstanceNumber.add = add;
// let addInstanceString = new addGenericsClass2<string>;
// addInstanceString.add = add;
let addInstanceNumber = new addGenericsClass2<number> ();
addInstanceNumber.add = add;
let addInstanceString = new addGenericsClass2<string> ();
addInstanceString.add = add;
addInstanceNumber.add(1, 2);
addInstanceString.add(`1`, `2`);
泛型是什么设计模式
模板方法模式
https://www.cnblogs.com/xgqfrms/p/14097237.html
refs
https://www.typescriptlang.org/docs/handbook/generics.html
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14099974.html
未经授权禁止转载,违者必究!