Skip to content

4.2 regression: TS2345: argument is not assignable to a parameter of a generic typeΒ #43152

@Igorbek

Description

@Igorbek

Bug Report

Mapped type's element is not assignable to type T | undefined where T is unconstrained

πŸ”Ž Search Terms

mapped types, TS2345, generic, not assignable

πŸ•— Version & Regression Information

Regression in TS 4.2.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type MyMap<M extends object> = {
    [K in keyof M]: {
        x: number
    }
}

declare function g<T>(value?: T): void;

function f<M extends object>(mymap: MyMap<M>, k: keyof any) {
    const elemofM = mymap[k as keyof M]
    g(elemofM)
    //~~~~~~~ TS2345: Argument of type '{ x: number; }' is not assignable to parameter of type 'MyMap<M>[keyof M]'.
}

πŸ™ Actual behavior

error TS2345: Argument of type '{ x: number; }' is not assignable to parameter of type 'MyMap[keyof M]'.

πŸ™‚ Expected behavior

No error (it was not in TS 4.1)

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