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

TypeScript emits d.ts file that is invalid for complex mapped type. #56718

Closed
CraigMacomber opened this issue Dec 9, 2023 · 0 comments · Fixed by #56742
Closed

TypeScript emits d.ts file that is invalid for complex mapped type. #56718

CraigMacomber opened this issue Dec 9, 2023 · 0 comments · Fixed by #56742
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@CraigMacomber
Copy link

CraigMacomber commented Dec 9, 2023

🔎 Search Terms

d.ts emit type error

🕗 Version & Regression Information

  • This is a compile error when consuming a d.ts file emitting by the same version and configuration of TypeScript
  • This repros in all versions I have tested (5.0.6, 5.1.6 and 5.3.2)
  • Earlier version error when compiling the input file, so they could not run this scenario.

⏯ Playground Link

https://www.typescriptlang.org/play?module=1#code/KYDwDg9gTgLgBAMwK4DsDGMCWEV2CpAWwDEoJCBlGKTFAcwGcAeNHB+AWWEICNgoGeEDHwATQVGABDUTgA2ATzjsa9ANoBdAHwAKAJRwA3gCg4cGArDA4ABSmwAjHAC8R02bhqA1sCW04PgoQCHBcvPyCUoJhfALevhpCIijiytS0dO4ecAD8odyxDPEKGlkeAFxwKMAAbvwalTERxRoA3O4Avu1mFla29jAATC5GnjZkVrB+uIHB-Y4N8zAOauMQkxaJXe6SMEhQuADyPABWwBgAdGiSUiI6BHJyBlFLg+0dxkA

💻 Code

export function enumFromStrings<const Members extends readonly string[]>() {
  type Part1 = {
    [key in keyof Members as Members[key] extends string
      ? Members[key]
      : never]: Members[key];
  };
  type Part2 = { [Property in keyof Part1]: Part1[Property] };
  return Object.create(null) as Part2;
}

🙁 Actual behavior

Emits d.ts:

export declare function enumFromStrings<const Members extends readonly string[]>(): { [Property in keyof { [key in keyof Members as Members[key] extends string ? Members[key] : never]: Members[key]; }]: { [key in keyof Members as Members[key] extends string ? Members[key] : never]: Members[key]; }[Property]; };

Which errors:

Type 'Property' cannot be used to index type '{ [key in keyof Members as Members[key] extends string ? Members[key] : never]: Members[key]; }'.

In playground

🙂 Expected behavior

I expect TypeScript to either error when trying to emit a d.ts file, or generate a valid d.ts file it can consume without error.

Additional information about the issue

I have a workaround for the case where I hit this, so I'm mainly reporting this incase it helps TypeScript development.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 11, 2023
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.4.0 milestone Dec 11, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 11, 2023
CraigMacomber added a commit to microsoft/FluidFramework that referenced this issue Jun 4, 2024
## Description

microsoft/TypeScript#56718 was fixed, but does
not fix our issue so
microsoft/TypeScript#58688 was filed and is
now referenced.

Also a few minor cleanups in the file I saw while working on it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants