Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS not able to infer that T[PropsThatExtend<T, M>] extends M #42795

Closed
Flonk opened this issue Feb 14, 2021 · 1 comment
Closed

TS not able to infer that T[PropsThatExtend<T, M>] extends M #42795

Flonk opened this issue Feb 14, 2021 · 1 comment

Comments

@Flonk
Copy link

Flonk commented Feb 14, 2021

Bug Report

πŸ”Ž Search Terms

  • type inference
  • mapped type
  • conditional types

πŸ•— Version & Regression Information

All versions I tested.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type PropsThatExtend<T, M> = {
  [K in keyof T]: T[K] extends M ? K : never;
}[keyof T];

const isFour = <T,>(obj: T, prop: PropsThatExtend<T, number>) => {
  return obj[prop] === 4;
}

type Foo = {
  bar: number;
  baz: number | string;
  qux: 2 | 4;
};

/* PropsThatExtend<Foo, number> === "bar" | "qux"; */

isFour({ bar: 1, baz: 2, qux: 4 } as Foo, "qux");

πŸ™ Actual behavior

Error on line 6:
This condition will always return 'false' since the types 'T[PropsThatExtend<T, number>]' and 'number' have no overlap.

πŸ™‚ Expected behavior

Since all members in T[PropsThatExtend<T, number>] extend number the compiler should be able to recognize that the two types being compared overlap.

@Flonk Flonk changed the title TS not able to infer that T[keyof PropsThatExtend<T, M>] extends M TS not able to infer that T[PropsThatExtend<T, M>] extends M Feb 14, 2021
@jcalz
Copy link
Contributor

jcalz commented Feb 14, 2021

Duplicate #30728

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants