microsoft / microsoft/TypeScript
Object properties are inferred in the wrong order: should infer properties with `NoInfer<T>` AFTER properties with `T`
Chưa có ai nhận issue này.
- 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ả
### 🔎 Search Terms
NoInfer
brittle inference
object properties
Related issues:
- https://github.com/microsoft/TypeScript/issues/56297 : inference depends on the order of the properties. This explains the inconsistency in my issue.
### 🕗 Version & Regression Information
Tested in 6.0.2 and nightly.
### ⏯ Playground Link
[Playground Link](https://www.typescriptlang.org/play/?noUnusedLocals=true&noUnusedParameters=true&exactOptionalPropertyTypes=true&noPropertyAccessFromIndexSignature=false&ts=6.0.0-dev.20260401#code/KYDwDg9gTgLgBDAnmYcASEIGsDOd8C8cASsAMbQAmAPDjFAJYB2A5gDRwAUAdLwIZQWOAFxw+TRAG0AugEo4BAHxiJigNwAoDUhRwA6gxgALDNhzUAKnFAxgTSnlO5lRAN5HMuURYC+mnagA4sAwTuZWNnYO+oYmnubiiIqKGviEcBEgtvZ4BsZh1MwAZsBQ6MoA-OhwokzAAG6lanBaAejxAExo4pQANqXmaNZZUY7xLnCuqWmSANJwzHBYwIgQRdV8eAAGACSuswBkdIysPlvSomhz0tP4APR3C+sA7qhQwAC2EI1ieEpwlAgcCYEHgz2gWBUiC+724Gj8WlAkFgCyYtigRT4ZFQAGEIGioBBev0oJZhtlomFlFM0nAPGZvJofBoyL1NngAFqTW50+IKSZFTCiTgAfQYogAzAAWWT-Vw+BHM7TIXH4+hEkkABUJ9QYlFK1BxAHkAHIWYhGgAyloAosRyaM4HiCRqDYlkvzODzaT7fX7fTABCwQpcLABZW39D52eAPJ5wV5wd5fH6GBZgiE4OH+nO5uDyf7Gs0W612xHgaDwNoANQYwGeADEsTBoIgAIKCcxF81W232yI5J1qwnEt2qCY0v0UF2jqDa7560qiZ3q2fz3X60ndkt9xRsHk+OAHbnetJ8GCBshGFcjkkVYWnvN+uMMF5vT7fVCGDiA4A4JgAOQZlAWDZk+ebTquJKiCaEAAJJMCUW6mj2pbECk4F+gWyj1BAer7rSh7HpqAgwAwfC9NQwShD0JI4A2hIfNQsEIUhhooTudrJBhaQvm+SYfj8JGwORlHKMYdgLPydSNGUnB6BYDYVLIrQqnA1HdPYdEMRATHbr2doOoON6uqS7o8ekYRdLRAxUSEBT6WhySaLSfEJqgACuOBBPZ8TscWBnoQoygMPyiRcApSkqRoSKVgCwCYh5vTwEUHlMGQZH4nAZDvOewC1vWOJsjg5iPphaSOX2RnRCZs7UO6ZVPooXpPiKQYiHABWNs2rYdkI-mobuZXyPKWh3AAVONdxaDlwB5V1RXspwk73I8r7uXAYAefAaZ8EU6JiBeWLXsOpmcLIHBpr+-5AQmEJgatd0gXg62Jt58BIr0DBkIYvSIAgRioG0axwCKZAPdlp1rjqi5QMKYPYcC9ZwBy537jybmJsmn7pnA0CbdtuN7QdgoQGjuPXYBwGgTy56XidM4kqKABeI0+ARaSk6KDBs-CshqEAA)
### 💻 Code
See playground for the full example.
```ts
type fooArgs = {
a: (_: A) => X,
// note: NoInfer> better than Y>, doesn't change this issue though
b: NoInfer>
}
function foo(args: fooArgs) {}
foo({
a: (_) => ...,
b: ... // inference of the value given to b is VERY brittle.
});
```
### 🙁 Actual behavior
Sometimes, `b` is inferred before `a`.
Then, as `a` is the one used to infer the generic parameter `T`, the value given to `b` doesn't have the correct type (because inferred before `a`).
This is likely due to the fact that the value given to `a` is a callback with a parameter we didn't specify the type (`(_) => ...`). Therefore, TS has first to look at `fooArgs` to infer the callback's parameter type (i.e. the type of `_`).
Due to that, `b` is sometime inferred before `a` has been properly inferred. This behavior depends on the order of the object properties (cf related issue). It is also influenced by other defined properties, and how they are defined. Making the inference quite chaotic.
### 🙂 Expected behavior
TS should infer `NoInfer` properties AFTER `T` properties, in order to prevent such issues.
### Additional information about the issue
The fact that the order of the inference changes is quite troublesome, hiding the issue in some cases.
Could be nice to have a kind of tool/flag to detect such kind of potential issues, and to help debugging.
_EDIT:_ A possible workaround:
```ts
type fooArgs = {
b: Y
}
function foo(a: (_: A) => X, args: NoInfer>) {}
foo(
a: (_) => ..., {
b: ...
});
```
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với Playground được liên kết và ví dụ fooArgs/foo đã được rút gọn, thay đổi thứ tự các thuộc tính của object và chú thích tham số của callback. So sánh hành vi với issue liên quan #56297, đồng thời theo dõi cách các thuộc tính NoInfer được suy luận. Được xem là hoàn thành khi ví dụ luôn suy luận T từ các thuộc tính chứa T trước các thuộc tính NoInfer, và hành vi phụ thuộc vào thứ tự đã được báo cáo được xử lý.
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
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 42/100