microsoft / microsoft/TypeScript
`as const` breaks intra expression inference
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
### 🔎 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_
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il TypeScript Playground collegato e confronta gli esempi `broken` e `working`, quindi traccia come l’asserzione `as const` influisce sul tipaggio contestuale e sull’inferenza per `produce`. Il lavoro è completato quando, nel caso con asserzione, il parametro del callback viene inferito senza `unknown` e viene mantenuta l’inferenza letterale prevista `100` per `D`.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100