[TypeScript] Dynamically initialize class properties using TypeScript decorators
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的更多相关文章
- 玩转TypeScript(引言&文章目录) --初看TypeScript.
JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
- electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- react: typescript project initialize
Initialize the project create a folder project Now we’ll turn this folder into an npm package. npm i ...
- [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 ...
- [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 ...
- [TypeScript] 1. Catching JavaScript Mistakes with TypeScript
The TypeScript compiler is a powerful tool which catches mistakes even in vanilla JavaScript. Try it ...
- TypeScript完全解读(26课时)_1.TypeScript完全解读-开发环境搭建
1.TypeScript完全解读-开发环境搭建 初始化项目 手动创建文件夹 D:\MyDemos\tsDemo\client-demo 用VSCode打开 npm init:初始化项目 然后我们的项目 ...
- TypeScript完全解读(26课时)_2.TypeScript完全解读-基础类型
2.TypeScript完全解读-基础类型 src下新建example文件夹并新建文件.basic-type.ts.截图中单词拼错了.后需注意一下是basic-type.ts 可以装tslint的插件 ...
- TypeScript完全解读(26课时)_4.TypeScript完全解读-接口
4.TypeScript完全解读-接口 初始化tslint tslint --init:初始化完成后会生成tslint.json的文件 如果我们涉及到一些规则都会在这个rules里面进行配置 安装ts ...
随机推荐
- Javascript传参参考
可参考的细节: <!doctype html> <html lang="en"> <head> <meta charset="U ...
- delphi.memory.分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同,内存分配函数
来自:http://www.cnblogs.com/qiusl/p/4028437.html?utm_source=tuicool&utm_medium=referral ---------- ...
- Solidity番外篇(一)Solidity在线or插件使用
在学习以太坊合约的过程中会需要自己编写智能合约,官方提供了几种方式供大家使用.下面分别简单介绍一下,如果有错误的地方,还留言指正补充. DAPP IDE 说实话,这个版本IDE我还没有使用过,只提供一 ...
- hdu4240 求一条流量最大的路/(此题网上百分之90以上算法是错误的)
题意:求最大流/一条流量最大的路的流量.(此题HDU上数据水,下面俩种错误的都能过....) 思路1;每次增广的时候更新流量,保存最大的那条. 错误性:每次更新,有可能最大的那条流量是前几次已经增广 ...
- LayerDate渲染多个class出现闪现问题的解决
填写表单的时候有时候会需要添加一行表单的业务逻辑,而表单要用到LayerDate的话便不可避免的出现多个class的情况 这种情况下后面的class是无法渲染的,layerDate官网提出了解决方法: ...
- swipper插件引起的a链接失效问题
在使用swiper过程中,发现a链接失效,此处没有效果,问题是 swiper是基于移动端触摸的,会有一个全局的click事件,这个事件屏蔽了A标签的链接,是为了防止手机滑动的时候不小心触发A标签而设定 ...
- 发布Office 365插件
在上一篇博客<VisualStudio 2013开发Office插件>开发完成了插件后,需要将插件发布 发布前需要: Azure 应用服务,作为Office插件的发布空间,地址是:http ...
- VisualStudio 2013开发Office插件
在VS中选择创建新项目,选择App for Office 选择mail出现的位置 Task pane The app appears in the task pane of a Microsift O ...
- [centos6.5]添加eclipse快捷方式
[Desktop Entry] Version=buzhidao Encoding=UTF-8 Name=eclipse Comment=eclipse-for-php Exec=/opt/eclip ...
- Codeforces 810 B. Summer sell-off
B. Summer sell-off time limit per test 1 second memory limit per test 256 megabytes input standard ...