Skip to content

Error using indexed access types with mapped typesΒ #49242

Closed
@irudoy

Description

@irudoy

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions