microsoft / microsoft/TypeScript

this type in conditional type false clause is incorrectly typed

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

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

Bug Domain: This-Typing
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ả

TypeScript Version: 3.7.3

Search Terms:
generic type this widening incorrect

Code

type Option1 = { run(this: { b: 1 }): void }

function f1(options: Option1) { return options }

// `this` is correctly typed as `{ b: 1 }`
f1({ run() { this.b } })

type Option2<Config> = Config extends Record<string, any> ? {
  config: Config,
  run(this: Config): void
} : {
  run(arg: { b: 1 }): void
}

function f2<Config>(options: Option2<Config>) { return options }

// `this` is correctly typed as `{ a: number }`
f2({ config: { a: 1 }, run() { this.a } })
// `arg` is correctly typed as `{ b: 1 }`
f2({ run(arg) { arg.b } })

type Option3<Config> = Config extends Record<string, any> ? {
  config: Config,
  run(this: Config): void
} : {
  run(this: { b: 1 }): void
}

function f3<Config>(options: Option3<Config>) { return options }

// `this` is correctly typed as `{ a: number }`
f3({ config: { a: 1 }, run() { this.a } })
// `this` is widen to what `Config` extends to. In this case `Record<string, any>`
f3({ run() { this.b } })

Expected behavior:
this in f3({ run() { this.b } }) should be typed as in this in f1() and arg in f2()

Actual behavior:
this in f3({ run() { this.b } }) is typed as the base type of Config (Record<string,any> in the example above)

Playground Link:
Playground Link

Related Issues:
Some maybe related issues:
https://github.com/microsoft/TypeScript/issues/32990
https://github.com/microsoft/TypeScript/issues/30152
https://github.com/microsoft/TypeScript/issues/13995

I also recall there were some discussion about type and Record<>, but that is in the gitter channel and I can't find relevant issue or doc about it.

I think this is different than the related issues above because this does not involve union type.

This is about the false condition of the conditional type Config extends Record<string, any> ? ... : ... do not use the generic type Config and should not be affected by it.

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

Tái hiện các ví dụ trong TypeScript Playground được liên kết, so sánh kiểu ngữ cảnh của this trong f1, f2f3. Điều tra việc định kiểu theo ngữ cảnh cho kiểu điều kiện đối với mệnh đề false; được xem là hoàn thành khi this của callback f3 được định kiểu là { b: 1 } thay vì kiểu cơ sở đã được mở rộng, mà không gây hồi quy cho các ví dụ khác.

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.