microsoft / microsoft/TypeScript
`as const` breaks intra expression inference
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
const context expression assertion inference type parameter
### 🕗 Version & Regression Information
- This is the behavior in every version I tried
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.0-dev.20250806#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwxAGcMAeAFQBp4ARAPgApYBzALngG8AoeeLVAA7IM7GgG4e8ATBzBkYEOyYw2tAJTwAvHXjkJvMHiLIAtovjLV5DdvipTAIxAwJAXzWiJXQ6hLwHMgDWIPiaBMQYDNy8-EIi8ACMAAxJlJLSsvLmljY6LAB0GDgAyhgw-MwMamkGRqbZLLnwBRAhzBgAFmmuzUTwPiRqXgMY8ADuODCBFVrhJFGSscLsyanpMnIKSo1aeSqFJWUVVTX9dWbbKk0tbZ3dQ1xAA
### 💻 Code
```ts
declare function test(arg: {
input: D;
produce: (arg: D) => T;
consume: (arg: T) => number;
}): D;
const broken = test({
input: 100,
produce: (arg) => arg.toString(), // 'arg' is of type 'unknown'.(18046)
consume: (arg) => arg.length,
} as const);
const working = test({
input: 100,
produce: (arg) => arg.toString(), // `arg` inferred as `number`
consume: (arg) => arg.length,
});
```
### 🙁 Actual behavior
`as const` is the only difference between those two and its presence breaks assigned parameter type in `produce`
### 🙂 Expected behavior
I think `as const` should not impact inference this way and both should be inferred just OK. It's just that the `broken` case should infer `100` instead of `number` for the `D` type parameter
### Additional information about the issue
_No response_
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 TypeScript Playground được liên kết và so sánh các ví dụ `broken` và `working`, sau đó theo dõi cách assertion `as const` ảnh hưởng đến contextual typing và suy luận kiểu cho `produce`. Được xem là hoàn tất khi trong trường hợp có assertion, tham số callback được suy luận mà không có `unknown` và vẫn giữ được suy luận literal `100` như mong đợi cho `D`.
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
- 42/100