microsoft / microsoft/TypeScript
ThisType for Ember.computed and Ember.observer
还没有人认领这个 Issue。
- 主要语言
- Go
- 星标
- 111k
- 派生
- 14.3k
- 平均合并
- 1 天 19 小时
- 30 天内合并 PR
- 117
描述
TypeScript Version: 2.3.4
Thanks for your work on this great project :)
Context
We are experimenting with TypeScript 2.x in Ember, and the new TypeScript 2.x features really enable us to type most of our Ember code. There is however one thing we came across that doesn't seem like something we can solve in our interfaces/declarations:
It seems that there currently is no way to get the this context inside the Ember.computed and Ember.observer pattern.
ThisType not compatible with Ember.computed and Ember.observer?
We have set up some experimental interfaces for Ember.Object.extend() (inspired by Vue.js' type declarations, @ember/types and various other sources)
https://github.com/draios/ember-typescript2/blob/master/app/types/ember/ember-types.d.ts#L78-L83
These types/interfaces are working so far; all methods and hooks have the proper this type, and class properties are accessible through getters and setters.
However when it comes to computed properties, there seems to be no way to get the proper this context inside the function passed to Ember.computed() (see example below).
Code
export default Ember.Object.extend({
myProp: true,
myMethod: function() {
// myProp is accessible because of ThisType
this.get('someProperty');
}
computedProperty: Ember.computed('someProperty', function() {
// myProp is not accessible, because `this` is set to type <any>
let myVal = this.get('someProperty');
// ... both `this` and `myVal` are of type <any>
// and no way to fix this from an external interface?
});
});
Is this Ember pattern supported somehow in TypeScript?
Thanks!
N.B. I tried something like the following, but it doesn't work (X becomes of type {}).
// the function passed to Ember.computed
type ComputedPropertyFunc<T, X> = (this: X) => T
// Ember.computed with one observed property and a function
function computed<T, X>(observedProperty1: string, fn: ComputedPropertyFunc<T, X>): T & ThisType<X>
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 issue 中链接的实验性 ember-types.d.ts 声明,以及示例中描述的 TypeScript 2.x ThisType 行为。确定 Ember.computed 和 Ember.observer 是否可以接收预期的上下文 this 类型;完成标准是记录受支持的用法,或确定所需的编译器或声明更改。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100