microsoft / microsoft/TypeScript

Object literals should have a `this` type too

Đang mở
#33,201 3 bình luận 1 reaction 0 người được giao Xem trên GitHub

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

Awaiting More Feedback Experimentation Needed 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: 3.7.0-dev.20190831

Search Terms: this return type object composing composition

Code

I tried writing some self-contained parts of objects as a kind of multiple-inheritance or object composition, which I would combine later, and ran into this case.

const a = {
	Clone() {
		return this;
	},
	x: 1,
};

const b = {
	Clone: a.Clone,
	y: 2,
};

console.log(b.Clone().x); // number, bad! Should be an error!
console.log(b.Clone().y); // compiler error, bad! This should be acceptable!

Expected behavior: this, the return type of Clone, should evaluate to typeof b in b.Clone()

Actual behavior: It evaluates to typeof a, since it was defined in a

This can be circumvented by defining this as a type parameter like so:

const a = {
	Clone<T>(this: T) {
		return this;
	},
	x: 1,
}

const b = {
	Clone: a.Clone,
	y: 2
};

console.log(b.Clone().x); // compiler error, good!
console.log(b.Clone().y); // works! Also good!

Another interesting problem at play here though, is that using a type parameter for this also means that TS doesn't know what could be inside of this. This means you have to manually specify whatever members you hope to have access to. It would be nice if you could do T extends typeof a and it would automatically drop all the members of a in there. Right now, it will error if you do this:

'a' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)

I think all methods like this should, therefore, be implicitly equivalent to the following:

const a = {
	Clone<T extends typeof a>(this: T) {
		console.log(this.x); // should work
		return this;
	},
	x: 1,
}

Then, b.Clone() would error since b is not a superset of a. Unless you did c = Object.assign({}, a, b), and did c.Clone()

Playground Link: https://www.typescriptlang.org/play/?target=6#code/MYewdgzgLgBAhjAvDA3gKAJAGEA24CmAFAJSqYYBO+UArhWDFABYCWEA3JgL4A0mAHgC4YARjRdOaUJFgAjJGWx4w+YXAB0uAnwwBPYQCZxk6RBA586vAHNCszcqLF1-YuxgB6DzBpgAJvgAZiwqflLgZhZWILb2Wiok6rpunt6gALYADiwWFDD4FBQgFEA

Related Issues: https://github.com/microsoft/TypeScript/issues/29122

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 ví dụ TypeScript Playground được liên kết và so sánh kiểu trả về được suy luận của Clone khi nó được sao chép từ a sang b. Đọc issue liên quan #29122 để hiểu bối cảnh của hành vi này. Hoàn thành nghĩa là xác định và triển khai việc liệu các phương thức của object literal có nên suy luận this từ cách sử dụng đối tượng chứa chúng hay không, đồng thời duy trì quyền truy cập hợp lệ vào các thành viên của đối tượng ban đầu.

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
30/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.