Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange runtime caching bug involving as const and const T generic #58351

Open
JosXa opened this issue Apr 29, 2024 · 1 comment Β· May be fixed by #58378
Open

Strange runtime caching bug involving as const and const T generic #58351

JosXa opened this issue Apr 29, 2024 · 1 comment Β· May be fixed by #58378
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@JosXa
Copy link

JosXa commented Apr 29, 2024

πŸ”Ž 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

https://www.typescriptlang.org/play/?target=99#code/C4TwDgpgBA8gRgKwgY2AMQE4HsC2BRAO2AwEsIBnAHgBUA+KAXimqggA9gICATcqDCAENuWAgBsQUANokCAMwgYoAaQiT2nHn3LFZAcygAfKAQCuOOIqNRyIC1jEAaKLIVKAaoLGmIAXSm+AFBQUAD8UADe0gDWai4EKmq+AFxQnt7QAL7BUKkEEABuigDcgYGgkIkg6T4ACoIkGFR0jJE5UsrxULEgWHLMKdLKztQdvr6lmVI9fQOlgexgWBjAUHKmBKgkolA4gmDwSKiEuhSUOciiOsysHFy8UFiIKMCOF1er1ACy+5Dct5oHlIdKQCAZjGYLFZjLZ7E4oBtogQsAB3Ai+N60AAUOSeCFS1DeIT2YEgGFSWKkgmccEGqmqXjqDSaNFoAEpGPRvr8INw3mzUocXphcCdSGduaTeQF6BEcgB6eUAAWA5AAtIsXhqMNglABBMQCYSSMDYMkSKAVXmWgAW2FMehttug5BIegIgmApgEOQEXowCSFqAAdHJsPgiOLyFig8Bg1xTtG8WzgySsSSyWy2ZMypcCNdONcmCTY2KyNGooJUgBGGmpABMUEyzkpbGcIF8HIY9CkbagHezZSAA

πŸ’» 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

@JosXa JosXa changed the title Strange runtime caching bug Strange runtime caching bug involving as const and const T generic Apr 29, 2024
@Andarist
Copy link
Contributor

Andarist commented Apr 29, 2024

A failing test case prepared based on the reported issue:

/// <reference path="fourslash.ts" />

// @strict: true
// @target: esnext
// @lib: esnext

//// 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];
////
//// declare function mapObjectEntries<
////   const T extends object,
////   const TMapped extends [string | number | symbol, unknown],
//// >(
////   obj: T,
////   mapper: ([a, b]: KeyValuePairs<T>) => TMapped,
//// ): ObjectFromEntries<TMapped[]>;
////
//// mapObjectEntries({ a: 1, b: 2 }, ([x, y]) => ["a/*1*/", y]);

verify.completions({
    marker: "1",
    exact: ["a"],
});
verify.getSemanticDiagnostics([]);

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels May 3, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
3 participants