-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
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:
vivaldi_gWZYW9aEDa.mp4
π 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
Metadata
Metadata
Assignees
Labels
Help WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases