[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& ...
随机推荐
- 升级Xcode 导致插件失效的解决的方法
我们在升级xcode的情况下,我们的一些第三方插件就会失效. 比方cocoapods,等比較重要的三方插件, 解决这个问题例如以下: 进入插件文件夹:~/Library/Application Sup ...
- 数据结构(Java语言)——LinkedList简单实现
下面是一个能够使用的LinkedList泛型类的实现.这里的链表类名为MyLinkedList,避免与类库中反复. MyLinkedList将作为双链表实现,并且保留到该表两端的引用.这样仅仅要操作发 ...
- angularjs1-4 事件指令
<div ng-app="myApp"> <div ng-controller="firstController"> <div n ...
- BZOJ1492:[NOI2007]货币兑换 (CDQ分治+斜率优化DP | splay动态维护凸包)
BZOJ1492:[NOI2007]货币兑换 题目传送门 [问题描述] 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和B纪念券(以下简称B券).每个持有金券的 ...
- asp.net website 单独编译某个页面,连带编译app_code
选中某一个页面,然后右键build page
- poj--3169--Layout(简单差分约束)
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9098 Accepted: 4347 Descriptio ...
- 利用CSS3中的clac()实现按照屏幕分辨率自适应宽度
1.简介 calc()看其外表像个函数.平时在制作页面的时候,总会碰到有的元素是100%的宽度(例如body元素).如果元素宽度为100%时,其自身不带其他盒模型属性设置还好,要是有别的,那将导致盒子 ...
- filezilla的root账户无法连接服务器解决办法
lz一直都是用filezilla上传文件到vm虚拟机的,用的是ubuntu14.04的系统.最近自己重新搭了lamp去做thinkphp的学习,lz有两个账户,一个是kin,另外一个是root.大家都 ...
- 调试相关blogs收集
Debug Diag官方blog https://blogs.msdn.microsoft.com/debugdiag/ Tess https://blogs.msdn.microsoft.com ...
- Hua Wei 机试题目一
一.身份证号码验证 题目描述: 我国公民的身份证号码特点如下:1. 长度为18位:2. 第1-17位只能为数字:3. 第18位可以是数字或者小写英文字母x.4. 身份证号码的第7~14位表示持有人生日 ...