microsoft / microsoft/TypeScript
Strange runtime caching bug involving `as const` and `const T` generic
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔎 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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verknüpften TypeScript Playground-Reproduktion und konzentriere dich auf die generische Signatur mit const T und die Auswirkung des Hinzufügens und Entfernens von as const. Verfolge den für das inkonsistente Ergebnis verantwortlichen Typechecker-Pfad und überprüfe, dass wiederholte Prüfungen dasselbe Ergebnis liefern, ohne sich auf die temporäre Assertion zu stützen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100