Skip to content

Should mapped types where the result is never filter keys?Β #23199

Closed

Description

I am not sure that this is a bug but it did behave in a way that I did not expect which is why I filed a new issue. If this is not a bug it should probably be closed and continued in earlier issues like #17678

TypeScript Version: 2.8.1

Search Terms:
Mapped types, never, filter, conditional types, Exclude, Omit, Pick

Code

type Fruit = { name: string };
type Apple = Fruit & { cultivar: string };

type Fruits = {
  banana: Fruit;
  alice: Apple;
  gala: Apple;
};

type Apples = {
  [T in keyof Fruits]: Fruits[T] extends Apple ? Fruits[T] : never
};

Expected behavior:
I expected Apples to have type

{
  alice: Apple;
  gala: Apple;
}

Actual behavior:
Apples have the type

{
  banana: never;
  alice: Apple;
  gala: Apple;
}

Having an object with a never key is quite strange to me. How would that object ever be created?

Playground Link: http://www.typescriptlang.org/play/#src=%0D%0Atype%20Fruit%20%3D%20%7B%20name%3A%20string%20%7D%3B%0D%0Atype%20Apple%20%3D%20Fruit%20%26%20%7B%20cultivar%3A%20string%20%7D%3B%0D%0A%0D%0Atype%20Fruits%20%3D%20%7B%0D%0A%20%20banana%3A%20Fruit%3B%0D%0A%20%20alice%3A%20Apple%3B%0D%0A%20%20gala%3A%20Apple%3B%0D%0A%7D%3B%0D%0A%0D%0Atype%20Apples%20%3D%20%7B%0D%0A%20%20%5BT%20in%20keyof%20Fruits%5D%3A%20Fruits%5BT%5D%20extends%20Apple%20%3F%20Fruits%5BT%5D%20%3A%20never%0D%0A%7D%3B%0D%0A

Related Issues:
If this have worked as expected #17678 would be already be supported. That issue is marked as a duplicate and linked to #12424 and #17636 but I am not sure I see the direct relation there to be honest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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