Microsoft/TypeScript
Ver no GitHubStrange runtime caching bug involving `as const` and `const T` generic
Open
#58.351 aberto em 29 de abr. de 2024
Domain: check: Error InstabilityHelp WantedPossible Improvement
Métricas do repositório
- Stars
- (48.455 stars)
- Métricas de merge de PR
- (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)
Description
🔎 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