[TypeScript] Generic Functions, class, Type Inference and Generics
Generic Fucntion:
For example we have a set of data and an function:
interface HasName {
name: string;
} const heros: HasName[] = [
{name: 'Jno'},
{name: 'Miw'},
{name: 'Ggr'},
{name: 'Gew'},
{name: 'Wfe'}
]; function cloneArray(ary: any[]): any[] {
return ary.slice();
} const clones = cloneArray(heros);
When we check the 'clones' type, you can see it is 'any[]'.
To add more type information we can change the function:
function cloneArray<T>(ary: T[]): T[] {
return ary.slice();
}
Now we get 'clones' type as 'HasName[]'.
Generic Class:
class SuperCharacter {
constructor(public name: string) { }
} class Hero extends SuperCharacter { } class SuperTeam {
constructor(public members: SuperCharacter[],
public leader: SuperCharacter
) { }
} const captainAmerica = new Hero('Captain America');
const thor = new Hero('Thor');
const ironMan = new Hero('IronMan'); const avengers = new SuperTeam(
[captainAmerica, thor, ironMan],
captainAmerica
); const members = avengers.members;
If we check 'avengers' type is 'SuperTeam'. 'memebers' type is 'SuperCharacter[]'.
To add more information to the types we can do:
class SuperTeam<T> {
constructor(public members: T[],
public leader: T
) { }
}
Now the 'avengers' type is 'SuperTeam<Hero>' and 'members' type is 'Hero[]'.
Now, let's say we have another class:
class Villain extends SuperCharacter { }
Have some members.
const luthor = new Villain('Luthor');
const bizarro = new Villain('Bizarro');
const captainCold = new Villain('Captain Cold'); const megaCrossoverTeam = new SuperTeam([
captainAmerica, thor, ironMan, luthor,
bizarro, captainCold
], captainAmerica);
'megaCrossoverTeam' is type of 'SuperTeam<Hero | Villain>'.
If we want to add some restrictions to the class, we can do:
class SuperTeam<T extends SuperCharacter> {
constructor(public members: T[],
public leader: T
) { }
}
Then the example below will throw error:
const avengers = new SuperTeam(
[, , ], );
Because the type is number.
[TypeScript] Generic Functions, class, Type Inference and Generics的更多相关文章
- [TypeScript] Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...
- 深入理解typescript的Functions
Functions Introduction # Functions are the fundamental building block of any application in JavaScri ...
- 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”
无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“Sy ...
- Type Safety and Type Inference
Swift is a type-safe language. A type safe language encourages you to be clear about the types of va ...
- Type inference
Type inference refers to the automatic detection of the data type of an expression in a programming ...
- [TypeScript] Union Types and Type Aliases in TypeScript
Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...
- [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript
Sometimes we might want to make a function more generic by having it accept a union of different typ ...
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- Generic method return type
Here's the Animal class: public class Animal{ private Map<String,Animal> friends =new HashMap& ...
随机推荐
- 用Python实现简单的服务器
socket接口是实际上是操作系统提供的系统调用.socket的使用并不局限于Python语言,你可以用C或者JAVA来写出同样的socket服务器,而所有语言使用socket的方式都类似(Apach ...
- VS2010编译器工具cl对c++11标准支持情况測试
本文探讨了VS2010编译工具cl对C++11标准的支持情况.提供了利用C++11新特性的两段代码来进行測试,并同g++ 4.9.3编译器的编译情况相对照.总的说来:VS2010的编译器工具cl部分支 ...
- CentOS 6 安装最新的 Redis 2.8 ,安装 TCMalloc
1,遇到的问题就是 redis 2.8 版本号依赖 Google 的 TCMalloc TCMalloc(Thread-Caching Malloc)是google开发的开源工具──"goo ...
- svn 服务器的搭建
SVN服务器运行模式:模式1:svn服务器单独运行 监听: 3690端口 访问: svn://IP模式2: svn 服务器+ apache : 80 端口 访问: http://IP ...
- 【为小白菜打call】
作为本校的竞赛生,我必须为我大OJ打call caioj,小白菜oj,顾名思义,就是为刚踏进OI的“小白菜”们准备的网站,里面包含了许多专题内容,各种模版和讲解视频 而且对于刚学习C++的同学,更有帮 ...
- [HDU 5542] The Battle of Chibi
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5542 [算法] 树状数组优化DP [代码] #include<bits/stdc++.h&g ...
- 杂项:ExtJS
ylbtech-杂项:ExtJS extjs是一种软件.自动生成行号,支持checkbox全选,动态选择显示哪些列,支持本地以及远程分页,可以对单元格按照自己的想法进行渲染,这些也算可以想到的功能. ...
- Oracle 查看锁定表用户的SQL
#描述:当执行如下SQL select * From XXX for Updata 发现表“XXX”被锁定 #只需要执行如下SQL 查看锁定该表的用户 SELECT object_name, mach ...
- Python笔记(七)
# -*-coding:utf-8-*- # Python 文件I/O # 打印到屏幕 #print 1234567 # 读取屏幕输入 #input_str=raw_input("Pleas ...
- C/C++函数调用约定与this指针
关于 C/C++ 函数调用约定,大多数时候并不会影响程序逻辑,但遇到跨语言编程时,了解一下还是有好处的. VC 中默认调用是 __cdecl 方式,Windows API 使用 __stdcall 调 ...