microsoft / microsoft/TypeScript
Strange runtime caching bug involving `as const` and `const T` generic
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
is:issue runtime caching as const
🕗 Version & Regression Information
- This is a type checker runtime inconsistency
- This is the behavior in every version I tried since const generics were introduced
⏯ Playground Link
💻 Code
type ObjectFromEntries<T> = T extends readonly [infer Key extends string | number | symbol, infer Value][]
? { [key in Key]: Value }
: never;
type KeyValuePairs<T> = {
[K in keyof T]: [K, T[K]];
}[keyof T];
export function mapObjectEntries<
const T extends object,
const TMapped extends [string | number | symbol, unknown],
>(
obj: T,
mapper: ([a, b]: KeyValuePairs<T>) => TMapped,
): ObjectFromEntries<TMapped[]> {
//@ts-expect-error Already properly typed through the signature
return Object.fromEntries(Object.entries(obj).map(mapper));
}
const test = mapObjectEntries({ a: 1, b: 2 }, ([x, y]) => [x, y]);
Here is a screencast illustrating the issue:
https://github.com/microsoft/TypeScript/assets/7313176/4875d16f-6a42-47a4-8f9c-2bd165de7a2c
🙁 Actual behavior
Type checker is context sensitive. When I add an as const behind the expression (see screencast), it compiles fine even after I remove the as const again.
🙂 Expected behavior
Consistency
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 la riproduzione collegata di TypeScript Playground, concentrandoti sulla firma generica che usa const T e sull’effetto dell’aggiunta e della rimozione di as const. Segui il percorso del type-checker responsabile del risultato incoerente e verifica che controlli ripetuti producano lo stesso risultato senza fare affidamento sull’assertion temporanea.
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
- 35/100