Skip to content

Cannot assign property to same type with generic key #32693

Open
@LinusU

Description

@LinusU

TypeScript Version: 3.6.0-dev.20190803

Search Terms: assigning keyof generic key

Code

type A = { a: number }
type B = { b: number }

interface Foo {
    a: A
    b: B
}

declare let target: Foo
declare let source: Foo
declare let key: keyof Foo

target[key] = source[key]

Expected behavior:

I expect it to allow the assignment. Since typeof target and typeof source are the same, I expect typeof target[key] to always be the same as typeof source[key].

Actual behavior:

error TS2322: Type 'A | B' is not assignable to type 'A & B'.

Playground Link: https://www.typescriptlang.org/play/#code/C4TwDgpgBAglC8UDeUCGAuKA7ArgWwCMIAnKAXwChRIoAhBZKAzXQk8iigSy2BIDNUAY2gAxAPbjkFKLLSYYMuczoVKFACYQhAG1TFoOiMCjB9Ac2OYJ4zdr0GoRkwGdxOYiOuS7u-YeMoAGsIEEwQkHF+KBtOM2JLYABtCIBdBjcPERTQ1IogA

Related Issues: #31665 (but it was closed and the comments seem to indicate that this should work 🤔) ping @RyanCavanaugh

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions