Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions when used correctly. In this lesson we will look at how we can use decorators to initialize properties of a class to promises that will make GET requests to certain URLs. We will also look at chaining multiple decorators to create powerful and versatile abstractions.

function Get(url) {
return function (target: any, name: string) {
// For future chain or cache on the same 'name'
const hiddenInstanceKey = "_$$" + name + "$$_";
const init = () => {
return fetch(url).then(response => response.json());
}; Object.defineProperty(target, name, {
get: function () {
return init();
},
configurable: true
});
}
} function First(num) {
return function(target: any, name: string) {
const hiddenInstanceKey = "_$$" + name + "$$_";
// access prvious getter on 'name'
const prevInit = Object.getOwnPropertyDescriptor(target, name).get;
const init = () => {
return prevInit()
.then(response => (response as any[]).slice(, num));
}; Object.defineProperty(target, name, {
get: function() {
return this[hiddenInstanceKey] || (this[hiddenInstanceKey] = init());
},
configurable: true
});
}
} class TodoService {
// Decorators runs from bottom to top
@First()
@Get('https://jsonplaceholder.typicode.com/todos')
todos: Promise<any[]>;
} const todoService = new TodoService();
todoService.todos.then(todos => {
console.log(todos);
})

[TypeScript] Dynamically initialize class properties using TypeScript decorators的更多相关文章

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

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

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

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

  3. react: typescript project initialize

    Initialize the project create a folder project Now we’ll turn this folder into an npm package. npm i ...

  4. [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript

    Conditional types take generics one step further and allow you to test for a specific condition, bas ...

  5. [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 ...

  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完全解读(26课时)_1.TypeScript完全解读-开发环境搭建

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

  8. TypeScript完全解读(26课时)_2.TypeScript完全解读-基础类型

    2.TypeScript完全解读-基础类型 src下新建example文件夹并新建文件.basic-type.ts.截图中单词拼错了.后需注意一下是basic-type.ts 可以装tslint的插件 ...

  9. TypeScript完全解读(26课时)_4.TypeScript完全解读-接口

    4.TypeScript完全解读-接口 初始化tslint tslint --init:初始化完成后会生成tslint.json的文件 如果我们涉及到一些规则都会在这个rules里面进行配置 安装ts ...

随机推荐

  1. HTML5-坦克大战一完成坦克上下左右移动的功能(一)

    坦克大战一完成坦克上下左右移动的功能 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  2. Spring MVC基础篇4

    Spring MVC操作原生Servlet 对象 Spring MVC 可以操作原生的Servlet API,如下的这些原生API,可以各自 自由混合使用,也可以和其他非原生 参数组合使用 实例代码: ...

  3. Selenium2+python自动化46-js解决click失效问题【转载】

    前言 有时候元素明明已经找到了,运行也没报错,点击后页面没任何反应.这种问题遇到了,是比较头疼的,因为没任何报错,只是click事件失效了. 本篇用2种方法解决这种诡异的点击事件失效问题 一.遇到的问 ...

  4. SQLAlchemy技术文档(中文版)-上

    转自:http://www.cnblogs.com/iwangzc/p/4112078.html 1.版本检查 import sqlalchemy sqlalchemy.__version__ 2.连 ...

  5. AC日记——[NOIP2015]运输计划 cogs 2109

    [NOIP2015] 运输计划 思路: 树剖+二分: 代码: #include <cstdio> #include <cstring> #include <iostrea ...

  6. 将win平台上的mysql数据复制到linux上报错Can't write; duplicate key in table

    将win平台上的mysql数据复制到linux上报错Can't write; duplicate key in table xxx 新年新气象,果然在新年的第一天就遇到了一个大坑,项目在win上跑的没 ...

  7. ZCMU训练赛-J(循环节+字符串处理)

    J - Java Beans There are N little kids sitting in a circle, each of them are carrying some java bean ...

  8. 洛谷——P1480 A/B Problem

    P1480 A/B Problem 题目描述 输入两个整数a,b,输出它们的商(a<=10^5000,b<=10^9) 输入输出格式 输入格式: 两行,第一行是被除数,第二行是除数. 输出 ...

  9. 【最近公共祖先】【线段树】URAL - 2109 - Tourism on Mars

    Few people know, but a long time ago a developed state existed on Mars. It consisted of n cities, nu ...

  10. 【暴力】洛谷 P2038 NOIP2014提高组 day2 T1 无线网络发射器选址

    暴力枚举. #include<cstdio> #include<algorithm> using namespace std; ][],d,n,x,y,z,num,ans=-; ...