microsoft / microsoft/TypeScript

ThisType for Ember.computed and Ember.observer

Aperta
#16,699 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Proposal Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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>

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia leggendo le dichiarazioni sperimentali di ember-types.d.ts collegate nella issue e il comportamento di ThisType di TypeScript 2.x descritto negli esempi. Determina se Ember.computed e Ember.observer possono ricevere il tipo this contestuale previsto; il lavoro è completato quando viene documentato l’uso supportato o vengono identificate le modifiche necessarie al compilatore o alle dichiarazioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.