Skip to content

generic bound doesn't work when used in index type with concrete index type #51394

@DetachHead

Description

@DetachHead

Bug Report

🔎 Search Terms

index type generic

🕗 Version & Regression Information

4.9.0-dev.20221025

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Foo {
  a: number[]
  b: string[]
}

declare const foo: Foo

const setValue = <T extends keyof Foo>(key: T, value: Foo[T][number]) => {
  foo[key] = [value]
}

🙁 Actual behavior

  Type '(string | number)[]' is not assignable to type 'Foo[T]'.
   Type '(string | number)[]' is not assignable to type 'number[] & string[]'.
     Type '(string | number)[]' is not assignable to type 'number[]'.
       Type 'string | number' is not assignable to type 'number'.
         Type 'string' is not assignable to type 'number'.

🙂 Expected behavior

no error, since it works when the type of value is Foo[T]:

const setValue = <T extends keyof Foo>(key: T, value: Foo[T]) => {
  foo[key] = value
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions