microsoft / microsoft/TypeScript

This-function parameters must be specified in caller *and* callee, even for methods

Đang mở
#10,288 5 bình luận 12 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.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

This means that only very careful people will benefit from assignability checking (and therefore argument checking at call sites). (Due to contextual typing, code that uses a this-typed library will get improved body checking a lot of the time.)

Specifically, assignability checking only happens when this is explicitly declared by both sides.

Instead, class methods should implicitly have this: this unless declared otherwise.

Given the declarations:

declare let f: (this: string, x: number) => void;
declare class C {
  m(x: number): void;
  n(this: this, x: number): void;
}
declare let c: C;

Expected behavior:

c.m = f // error, 'this: string' is not assignable to 'this: string'
f = c.m // error, 'this: C' is not assignable to 'this: string'
f = c.n // error, 'this: string' is not assignable to 'this: C'
c.n = f // error, 'this: C' is not assignable to 'this: string'

Actual behavior:

c.m = f // no error
f = c.m // no error
f = c.n // error, 'this: string' is not assignable to 'this: C'
c.n = f // error, 'this: C' is not assignable to 'this: string'

Comments

The "this parameter" feature was designed this way to balance four concerns:

  1. Backward compatibility with un-annotated code.
  2. Compiler efficiency.
  3. Consistency between class and interface this.
  4. Quality of error checking.

As you can see, (4) took the biggest hit by requiring annotation on both the source and target of the assignment.

Backward compatibility

The problem with unannotated code is that we can't tell whether a function is intended to be called with or without this.

Functions

--noImplicitThis helps address this problem by forcing annotation of this parameters for functions.

TODO: Work through some examples.

Methods

Actually, for methods, we do have a pretty good idea that a method is not supposed to be removed from its class. Even if the class implements an interface, we the class methods should still be able to refer to class-private implementation details.

TODO: Work through some examples.

Interface consistency

The problem is that interfaces are frequently used to interact with objects instead of the class type itself. But interfaces are used in lots of other contexts as well. That means we can't infer intent with an interface, but if we don't add this: this to interface methods, we're left with a bad assignment problem:

interface I {
  m(x: number): void;
}
class C implements I {
  y = 12;
  m(x: number) {
    return this.y + x;
  }
}
let c = new C();
f = c.m;
f(); // error, this must of type 'C'
let i: I = c;
f = i.m;
f(); // ok, this is not annotated

"Assignment escape hatches" like this exist throughout TypeScript, but adding more is not a good thing.

Efficiency

Briefly, if every interface has a this type, then every interface will be generic, even in non-OO code. This causes the compiler to generate about double the number of types internally, which is slower and uses more memory than today.

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 với các ví dụ về khả năng gán trong issue và theo dõi cách các tham số this tường minh được kiểm tra đối với các phương thức, lớp và interface. Một thay đổi hoàn chỉnh sẽ tạo ra bốn lỗi dự kiến, đồng thời tính đến khả năng tương thích ngược, tính nhất quán của interface, hiệu quả của trình biên dịch và các ví dụ chưa được giải quyết được đánh dấu TODO.

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
Khá rõ ràng
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.