microsoft / microsoft/TypeScript
ThisType for Ember.computed and Ember.observer
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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>
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the experimental ember-types.d.ts declarations linked in the issue and the TypeScript 2.x ThisType behavior described in the examples. Determine whether Ember.computed and Ember.observer can receive the intended contextual this type; done means documenting supported usage or identifying the compiler or declaration changes required.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100