microsoft / microsoft/TypeScript

ES6 Class: `asserts this is Type` raises error when variable containing instance is type narrowed

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

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

Domain: This-Typing Help Wanted Possible Improvement
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ả

Bug Report

🔎 Search Terms
  • Error: "Assertions require every name in the call target to be declared with an explicit type annotation"
  • Assertation function as class method
  • Type narrowing class and then calling assertation method
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about this
⏯ Playground Link

Playground link with relevant code

💻 Code
class MyClass {
	public assertIsMyClass(): asserts this is MyClass {
		if (!(this instanceof MyClass)) {
			throw new Error('this was not of type MyClass');
		}
	}
}

const maybeMyClass: MyClass | undefined = new MyClass();

if (maybeMyClass) {
	// This fails with error:
	// Assertions require every name in the call target to be declared with an explicit type annotation.(2775)
	maybeMyClass.assertIsMyClass();

	// But if we simply assign to a new variable without the optionality, it's fine:

	// this line works because we have narrowed the type through the if statement above
	const definitelyMyClass: MyClass = maybeMyClass; 
	// This raises no error even though it's equivalent to the first attempt
	definitelyMyClass.assertIsMyClass();
}
🙁 Actual behavior

Calling the class method assertation function raises error: "Assertions require every name in the call target to be declared with an explicit type annotation.(2775)"

🙂 Expected behavior

It should pass validation and narrow to the type given in the assertation.
This is because it will have this behavior if you simply assign to a new variable with a new type annotation (but which is equivalent to the narrowed type at that point).

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 liên kết Playground được cung cấp và tái hiện chẩn đoán trong mẫu mã, so sánh biến đã được thu hẹp kiểu với biến được chú thích rõ ràng. Theo dõi quá trình xác thực lệnh gọi assertion trong trình biên dịch TypeScript và thêm coverage cho trường hợp phương thức lớp này. Công việc được hoàn tất khi lệnh gọi đã được thu hẹp kiểu vượt qua bước xác thực và các kiểm tra assertion hiện có vẫn đúng.

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
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
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
35/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.