You can create an easy to chain API using TypeScript classes. Learn about the thisreturn type annotation and how it plays with function chaining and class inheritance.

class Adder {
protected acc: number = ;
add(num: number): Adder {
this.acc += num;
return this; // enable to chain methods
} get result() {
return this.acc;
}
} const adder = new Adder()
const res = adder.add().add().result;
console.log(res); // class Calculator extends Adder {
subtract(num: number): Calculator {
this.acc -= num;
return this;
}
} const cal = new Calculator();
const res2 = cal.add().add().subtract().result;
console.log(res2) // -97

You can also do:

const res2 = new Calculator()
.add()
.add()
.subtract()
.result;
console.log(res2) // -97

[TypeScript] Create a fluent API using TypeScript classes的更多相关文章

  1. [Vuex] Create a Vuex Store using TypeScript

    A Vuex store centralizes the state of your app, making it easy to reason about your state flow. In t ...

  2. [TypeScript] Create random integers in a given range

    Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int betwee ...

  3. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  4. [转]Entity Framework Fluent API - Configuring and Mapping Properties and Types

    本文转自:https://msdn.microsoft.com/en-us/data/jj591617#1.2 When working with Entity Framework Code Firs ...

  5. Entity Framework Code-First(10):Fluent API

    Fluent API in Code-First: We have seen different DataAnnotations attributes in the previous sections ...

  6. 8.Fluent API in Code-First【Code-First系列】

    在前面的章节中,我们已经看到了各种不同的数据注解特性.现在我们来学习一下Fluent API. Fluent API是另外一种配置领域类的方式,它提供了更多的配置相比数据注解特性. Mappings[ ...

  7. EF框架step by step(9)—Code First Fluent API

    在上一篇中,讲述了用数据特性的方式来标识实体与数据表之间的映射关系,在Code First方法中,还可以通过Fluent API的方式来处理实体与数据表之间的映射关系. 要使用Fluent API必须 ...

  8. 002.Create a web API with ASP.NET Core MVC and Visual Studio for Windows -- 【在windows上用vs与asp.net core mvc 创建一个 web api 程序】

    Create a web API with ASP.NET Core MVC and Visual Studio for Windows 在windows上用vs与asp.net core mvc 创 ...

  9. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

随机推荐

  1. 今天发现里一个非常好用的Listbox自绘类,带不同文字字体和图片,觉得很有必要记下来

    代码简写 MyListBox.h class CUseListBox : public CListBox { typedef struct _ListBox_Data { CString strApp ...

  2. HDU——T 1576 A/B

    http://acm.hdu.edu.cn/showproblem.php?pid=1576 Time Limit: 1000/1000 MS (Java/Others)    Memory Limi ...

  3. IOS开发UI基础--数据刷新

    IOS开发UI基础--数据刷新 cell的数据刷新包括下面几个方面 加入数据 删除数据 更改数据 全局刷新方法(最经常使用) [self.tableView reloadData]; // 屏幕上的全 ...

  4. 【Unity3D自学记录】鼠标移动三维物体

    创建一个脚本.例如以下: using UnityEngine; using System.Collections; public class OnMouse : MonoBehaviour { IEn ...

  5. VA对于开发QT是神器,VA自动补全QT

    我怎么就忘了,VA也可以适用于VS下开发QT程序.其中QT的头文件自己增加,主要是: C:\Qt\4.8.6_2008\include 但还有一些特殊类不认识,所以还得继续增加: C:\Qt\4.8. ...

  6. 修改shm,oracle11g需要扩大共享内存

    作者:david_zhang@sh [转载时请以超链接形式标明文章] 链接:http://www.cnblogs.com/david-zhang-index/archive/2012/04/26/24 ...

  7. ARCGIS刷新的故事

    转自原文章ARCGIS部分刷新 1, IActiveView.Refresh 全局刷新,即重绘地图中的所有内容,是效率最低的一种刷新方法.当数据量大时非常耗时.所以除非绝对必要,一般推荐使用IActi ...

  8. 例说linux内核与应用数据通信(一):加入一个系统调用

    [版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet,文章仅供学习交流,请勿用于商业用途]         应用不能訪问内核的内存空间.为了应用和内核交互信息,内核提供一 ...

  9. 当数据库没有备份,redo或undo损坏

    数据库在没有备份的情况下,如果数据库redo或undo损坏,可以通过如下方法处理,但是不一定成功 把init文件中的: undo_management=manual 然后启动数据库到mount 状态后 ...

  10. 11.5 Android显示系统框架_Vsync机制_黄油计划_三个方法改进显示系统

    5. Vsync机制5.1 黄油计划_三个方法改进显示系统vsync, triple buffering, vsync虚拟化 参考文档:林学森 <深入理解Android内核设计思想>第2版 ...