Skip to content

Two level deep indexed access: read works, write fails #47352

Description

@quixot1c

Bug Report

Reading from a two level deep Indexed Access type works, but writing gives "Type cannot be used to index".

🔎 Search Terms

  • Indexed Access
  • Mapped Type

🕗 Version & Regression Information

  • This changed between versions 3.3.3 and 3.5.1

⏯ Playground Link

Playground link with relevant code

💻 Code

enum A {
    First = "1",
    Second = "2"
}

type Values = { [a in A]: { [b: string]: string }}
const values: Values = {} as any;

function test<T extends A>(a: T, b: string) {
    const valueA = values[a];
    values[a] = valueA;

    const valueB = values[a][b]; // indexing Values[T] with string works
    values[a][b] = valueB; // => Type 'string' cannot be used to index type 'Values[T]'    
}

🙁 Actual behavior

Indexing a type while reading works, while writing fails with the same indexer.

🙂 Expected behavior

Indexed reads and writes both work when using the same indexer.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions