microsoft / microsoft/TypeScript

ThisType for Ember.computed and Ember.observer

Offen
#16,699 6 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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>

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit der Lektüre der in der Issue verlinkten experimentellen ember-types.d.ts-Deklarationen und des in den Beispielen beschriebenen ThisType-Verhaltens von TypeScript 2.x. Ermitteln Sie, ob Ember.computed und Ember.observer den vorgesehenen kontextuellen this-Typ erhalten können; abgeschlossen ist dies, wenn die unterstützte Verwendung dokumentiert oder die erforderlichen Compiler- oder Deklarationsänderungen identifiziert wurden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.