microsoft / microsoft/TypeScript

ThisType for Ember.computed and Ember.observer

Đang mở
#16,699 6 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Needs Proposal Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 19 giờ
Pull request đã merge (30 ngày)
117

Mô tả

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>

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách đọc các khai báo thử nghiệm trong ember-types.d.ts được liên kết trong issue và hành vi ThisType của TypeScript 2.x được mô tả trong các ví dụ. Xác định xem Ember.computed và Ember.observer có thể nhận kiểu this theo ngữ cảnh dự kiến hay không; hoàn tất khi đã ghi lại cách sử dụng được hỗ trợ hoặc xác định các thay đổi cần thiết đối với trình biên dịch hay các khai báo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.