Skip to content

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

Closed
@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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions