Skip to content

Indexing mapped type with keyof type param #16018

@liam-goodacre-hpe

Description

@liam-goodacre-hpe

TypeScript Version: 2.2.2

Code

On playground

type Eg = { foo: string, bar: number }

type VariantRec<T> = {[K in keyof T]: { type: K, value: T[K] }}
type Alpha1 = VariantRec<Eg>[keyof Eg]

type VariantType<T> = VariantRec<T>[keyof T]
type Alpha2 = VariantType<Eg>

const assignable12: Alpha2 = null as Alpha1
const assignable21: Alpha1 = null as Alpha2

Expected behavior:

Types Alpha1 and Alpha2 should be the identical.

Actual behavior:

Alpha2 incorrectly becomes: { type: 'foo' | 'bar', value: string | number }
It should be: { type: 'foo', value: string } | { type: 'bar', value: number }

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