microsoft / microsoft/TypeScript

ThisType for Ember.computed and Ember.observer

Abierto
#16,699 6 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Needs Proposal Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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>

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza leyendo las declaraciones experimentales de ember-types.d.ts enlazadas en la issue y el comportamiento de ThisType de TypeScript 2.x descrito en los ejemplos. Determina si Ember.computed y Ember.observer pueden recibir el tipo this contextual previsto; se considera terminado cuando se documente el uso compatible o se identifiquen los cambios necesarios en el compilador o en las declaraciones.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.