TypeScript Generics All In one

TypeScript 泛型

  1. 代码逻辑复用
  2. 扩展性
  3. 设计模式

方法覆写, 直接覆盖

方法重载,参数个数或参数类型不同

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 发布文章使用:只允许注册用户才可以访问!


TypeScript Generics All In one的更多相关文章

  1. TypeScript Generics

    TypeScript Generics https://www.typescriptlang.org/docs/handbook/generics.html 泛型 1 Generic Interfac ...

  2. [Typescript] Generics using TypeScript

    In this lesson we cover the key reason why programming languages need generics. We then show how use ...

  3. TypeScript Generics(泛型)

    软件工程的一个主要部分就是构建组件,构建的组件不仅需要具有明确的定义和统一的接口,同时也需要组件可复用.支持现有的数据类型和将来添加的数据类型的组件为大型软件系统的开发过程提供很好的灵活性. 在C#和 ...

  4. [TypeScript] The Basics of Generics in TypeScript

    It can be painful to write the same function repeatedly with different types. Typescript generics al ...

  5. 我要涨知识 —— TypeScript 常见面试题(一)

    1.ts 中的 any 和 unknown 有什么区别? unknown 和 any 的主要区别是 unknown 类型会更加严格:在对 unknown 类型的值执行大多数操作之前,我们必须进行某种形 ...

  6. linqjs

    Project Descriptionlinq.js - LINQ for JavaScript Features implement all .NET 4.0 methods and many ex ...

  7. Dart Generic All In One

    Dart Generic All In One Dart 泛型 https://dart.dev/guides/language/language-tour#generics /** * * @aut ...

  8. [TypeScript] Understanding Generics with RxJS

    Libraries such as RxJS use generics heavily in their definition files to describe how types flow thr ...

  9. [Typescript] Introduction to Generics in Typescript

    If Typescript is the first language in which you've encountered generics, the concept can be quite d ...

随机推荐

  1. centos7制作U盘启动盘-九五小庞

    一.准备相关软件 1.8G以上U盘 2.UltraISO虚拟光驱(试用版即可)最新版 下载地址:https://cn.ultraiso.net/xiazai.html  点击下载试用 3.CentOS ...

  2. 基于Vue的npm组件库

    前言(*❦ω❦) 思维导图可能有点高糊,有点太大了,项目和导图文件放到github或giteee上,这个思维导图也是我文章的架构,思维导图是用FeHelper插件生成的,这个是一款开源chrome插件 ...

  3. Memcached与Redis对比及其优劣分析

    国外讨论 本文主要总结缓存Redis和Memcached的区别,总结之前先参考外国知乎上的一篇问答:<Is memcached a dinosaur in comparison to Redis ...

  4. Git:.gitignore和.gitkeep文件的使用 让空文件夹被跟踪

    Git:.gitignore和.gitkeep文件的使用 Git:.gitignore和.gitkeep文件的使用 https://majing.io/posts/10000001781172 .gi ...

  5. 并发安全 sync.Map

    https://mp.weixin.qq.com/s/MqPm7QH3_D9roVkpTs9Xpw 谈谈Go的并发安全相关 原创 歪鼻子 歪鼻子 2020-12-27     package main ...

  6. SpringMVC听课笔记(二:SpringMVC的 HelloWorld)

    1.如何建Maven web项目,请看http://how2j.cn/k/maven/maven-eclipse-web-project/1334.html 2.Maven项目,pom文件中的jar包 ...

  7. php文件下载的实现(header)

    php文件下载的实现(header) $file_xls=$path;    //   文件的保存路径 $example_name=basename($file_xls);  //获取文件名   he ...

  8. python爬虫selenium相关

    首先上很好用的selenium中文文档,基本上所有问题都能通过阅读此文档解决.可惜好像没找到翻译者名称. https://python-selenium-zh.readthedocs.io/zh_CN ...

  9. Codeforces Round #666 (Div. 2)

    比赛链接:https://codeforces.com/contest/1397 A. Juggling Letters 题意 给出 $n$ 个字符串,可在字符串间任意移动字母,问最终能否使这 $n$ ...

  10. 【bzoj 2163】复杂的大门(算法效率--拆点+贪心)

    题目:你去找某bm玩,到了门口才发现要打开他家的大门不是一件容易的事-- 他家的大门外有n个站台,用1到n的正整数编号.你需要对每个站台访问一定次数以后大门才能开启.站台之间有m个单向的传送门,通过传 ...