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

Error using indexed access types with mapped types #49242

Closed
irudoy opened this issue May 25, 2022 · 2 comments Β· Fixed by #49341
Closed

Error using indexed access types with mapped types #49242

irudoy opened this issue May 25, 2022 · 2 comments Β· Fixed by #49341
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@irudoy
Copy link

irudoy commented May 25, 2022

Bug Report

πŸ”Ž Search Terms

indexed access types, mapped types

πŸ•— Version & Regression Information

  • This changed between versions 4.6.4 and 4.7.0

⏯ Playground Link

v4.8.0-dev.20220525 ❌

v4.7.0-beta ❌

v4.6.4 βœ…

v4.5.5 ❌

πŸ’» Code

type Types = {
  first: { a1: true };
  second: { a2: true };
  third: { a3: true };
}

class Test {
  entries: { [T in keyof Types]?: Types[T][] }

  constructor() {
    this.entries = {};
  }

  addEntry<T extends keyof Types>(name: T, entry: Types[T]) {
    if (!this.entries[name]) {
      this.entries[name] = [];
    }

    this.entries[name]?.push(entry); // error
  }
}

πŸ™ Actual behavior

Got the following error:

Argument of type '{ a1: true; } | { a2: true; } | { a3: true; }' is not assignable to parameter of type '{ a1: true; } & { a2: true; } & { a3: true; }'.
  Type '{ a1: true; }' is not assignable to type '{ a1: true; } & { a2: true; } & { a3: true; }'.
    Property 'a2' is missing in type '{ a1: true; }' but required in type '{ a2: true; }'

πŸ™‚ Expected behavior

No error. Or maybe I missed something, and this was intentional.

Seems like it was fixed in #47109 and broken in v4.7.

@jcalz
Copy link
Contributor

jcalz commented May 27, 2022

Looks like something's going on when the mapped type is optional. This happened between 4.7.0-dev.20220302 and 4.7.0-dev.20220329. It would definitely be nice if this didn't stay broken in 4.7.x for long.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 1, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.8.0 milestone Jun 1, 2022
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Jun 1, 2022
@ahejlsberg
Copy link
Member

Yeah, this is a bug. We're being a bit too conservative in the isMappedTypeGenericIndexedAccess function. It disallows any ? modifiers when really it should only disallow -?. Easy fix, I'll have a PR up soon.

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.

6 participants