[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& ...
随机推荐
- [SharePoint2010开发入门经典]SPS2010开发工具
本章概要: 1.了解不同的开发SPS的方法 2.了解SPS开发工具和环境 3.使用VS2010和SPD还有Blend开发SPS
- HDU 1757
简单的矩阵构造题,参看我前几篇的谈到的矩阵的构造法. #include <iostream> #include <cstdio> #include <cstring> ...
- Hive不同文件的读取与序列化
Hive不同文件的读取对照 stored as textfile 直接查看hdfs hadoop fs -text hive> create table test_txt(name string ...
- insmod: error inserting 'hello.ko': -1 Invalid module format
在学习编写linux驱动程序的时候,一般都是从写一个helloworld的模块開始. 可是在编译完毕后,进行模块载入的时候,有时会出现例如以下错误: insmod: error inserting ' ...
- EM无法登录,提示ORA-28001: the password has expired (DBD ERROR: OCISessionBegin)
--查看数据库目前的口令期限 sys@TESTDB11>select * from dba_profiles where profile = 'DEFAULT' and resource_nam ...
- 本地PC安裝Centos 6.5 操作手冊
http://www.xlgps.com/article/130038.html 一.准备工作 1.下载Centos6.5 ISO文件 我在官网上下的6.5版本CentOS-6.5-x86_64-bi ...
- 微信小程序 input使用letter-spacing失效问题
根据ui设计稿, 本来思路是一个input搞定,下面的线使用背景图 background:url('/images/line.png')no-repeat bottom center; 然后使用let ...
- js文字排序的方法
拼音排序: , b: , b: , b: , b: , b: , b: , b: "不" }]; arr.sort( function compareFunction(param1 ...
- js获取当前位置的地理坐标(经纬度)
在 freecodecamp 上学习时,碰到获取地理坐标问题.写个笔记纪录下. if(navigator.geolocation) { navigator.geolocation.getCurrent ...
- Java基础——Servlet
什么是Servlet Servlet是Java Web的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet中通常需要: l ...