It can be painful to write the same function repeatedly with different types. Typescript generics allow us to write 1 function and maintain whatever type(s) our function is given. This lesson covers syntax and a basic use case for Typescript generics.

We have a reusable function that pushes something into a collection, and then console logs the collection. We have two objects, and two arrays. We can just call the function with anything an array,

function pushSomethingIntoCollection(something, collection) {
collection.push(something);
console.log(collection);
} let jeanGrey = { name: "Jean Grey" };
let wolverine = { name: "Wolverine" }; let superHeroes = [jeanGrey];
let powers = ["telekinesis", "esp"]; pushSomethingIntoCollection("cool", superHeroes);
pushSomethingIntoCollection("adamantium claws", []);

but we're human and we make errors. What we want is to make sure we're pushing the right something, into the right array.

We can use a generic to tell the compiler, we're going to be using a specific type, but we're not going to tell you what that type is until the function is called. This is what a generic looks like. It doesn't matter what's between the angle brackets, as long as it makes sense to you.

function pushSomethingIntoCollection<T>(something: T, collection: T[]) 

Now if we do:

pushSomethingIntoCollection("meh", superHeroes);

IDE will show us error, because 'something: T' is string type, then collection should be array to string type.

But 'superHeros' is array of object type.

interface SuperHero {name: string;}

pushSomethingIntoCollection<SuperHero>("meh", superHeroes); //Error
pushSomethingIntoCollection<string>("adamantium claws", []); //OK

We can provide interface to tell IDE what generice type should be. So IDE can help to catch error.

[TypeScript] The Basics of Generics in TypeScript的更多相关文章

  1. 玩转TypeScript(引言&文章目录) --初看TypeScript.

    JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...

  2. electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  3. [Typescript] Introduction to Generics in Typescript

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

  4. [Typescript] Generics using TypeScript

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

  5. TypeScript完全解读(26课时)_6.TypeScript完全解读-泛型

    6.TypeScript完全解读-泛型 创建实例ts文件generics.ts 在index.ts内引入 fill是填充数组,创建的数组的元素数是times,填充的值就是接收的value的值 这里传入 ...

  6. [TypeScript] 1. Catching JavaScript Mistakes with TypeScript

    The TypeScript compiler is a powerful tool which catches mistakes even in vanilla JavaScript. Try it ...

  7. [TypeScript] Work with DOM Elements in TypeScript using Type Assertions

    The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...

  8. [TypeScript] Dynamically initialize class properties using TypeScript decorators

    Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions w ...

  9. TypeScript完全解读(26课时)_1.TypeScript完全解读-开发环境搭建

    1.TypeScript完全解读-开发环境搭建 初始化项目 手动创建文件夹 D:\MyDemos\tsDemo\client-demo 用VSCode打开 npm init:初始化项目 然后我们的项目 ...

随机推荐

  1. C#使用一般处理程序(ashx)中session

    .ashx中引用 session必须 using System.Web.SessionState ,继承IReadOnlySessionState/IRequiresSessionState IRea ...

  2. pyspark使用

    1.安装python3 2.idea安装Python插件 3.下载spark,设置SPARK_HOME环境变量 4.安装pyspark,numpy 5.运行pyspark应用 pyspark应用如果使 ...

  3. css3--根据数据加载显示的一个动画

    css: .circle { width: 200px; height: 200px; position: absolute; border-radius: 50%; background: #0cc ...

  4. 【Codeforces Round #427 (Div. 2) C】Star sky

    [Link]:http://codeforces.com/contest/835/problem/C [Description] 给你n个星星的坐标(xi,yi); 第i个星星在第t秒,闪烁值变为(s ...

  5. C++ static 静态成员变量 和 静态成员函数

    静态(static) 成员 变量 1•  静态成员变量的初始化须要在类外完毕. 2•  静态成员不属于详细的某个对象,而属于整个类: 3•  全部对象共享本类中的静态成员: 4•  静态成员最好直接通 ...

  6. CMake设置生成vs工程的动态库输出路径

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在网上搜了很多的资料,发现CMake不能设置一个动态库工程的输出目录和中间目录,难道除了VC之外其它编译器如gcc中没 ...

  7. ubuntu14.04 printk()默认打印的位置

    tail /var/log/syslog 即可显示printk打印的信息

  8. django 简单会议室预约(6)

    后台完了现在来看前端,前端用了一个bootstrap框架,看起来能好看点 先看一下文件结构:在djapp里创建了两个文件夹templates和static templates里面是要显示的页面,sta ...

  9. 洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes

    P1207 [USACO1.2]双重回文数 Dual Palindromes 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而7777 ...

  10. actionbar spinner-用法实例

    今天需要更改一个actionbar上的spinner的字体颜色,结果试了好长时间都没有解决,最后才发现,原来他是在代码下增加的一个textview,然后使用adapter加载的,并不是直接用frame ...