Skip to content

Mapped type index access does not work well with Extract utility typeΒ #50103

Closed
@Harpush

Description

@Harpush

Bug Report

πŸ”Ž Search Terms

indexed access types

πŸ•— Version & Regression Information

Not a regression

⏯ Playground Link

link

πŸ’» Code

enum Type {
  A = 'a',
  B = 'b'
}

interface One {
  type: Type.A;
  x: number;
}

interface Two {
  type: Type.B;
  y: string;
}

type Both = One | Two;

type GetByType<T extends Type> = Extract<Both, {type: T}>;

const mapper: {[P in Type]: (val: GetByType<P>) => boolean} = {/* ... */};

// Doesn't work :(
const getIt = <T extends Both>(b: T) => mapper[b.type](b);
// Works?
const getIt2 = <T extends Type>(b: T, c: GetByType<T>) => mapper[b](c);
// Doesn't work :(
const getIt3 = <T extends Type>(b: GetByType<T>) => mapper[b.type](b);

πŸ™ Actual behavior

getIt and getIt3 doesn't compile although somehow getIt2 compiles.

πŸ™‚ Expected behavior

All three examples should compile or at least getIt3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions