Skip to content

Typeguard should use conditional types #23764

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

Closed
mkulke opened this issue Apr 28, 2018 · 5 comments
Closed

Typeguard should use conditional types #23764

mkulke opened this issue Apr 28, 2018 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@mkulke
Copy link
Contributor

mkulke commented Apr 28, 2018

TypeScript Version: 2.9.0-dev.20180428

Search Terms:

conditional types, type guards

Code

type Pred<T, U extends T> = (v: T) => v is U;
function someFn<T, U extends T>(pred: Pred<T, U>, val: T): void {
  if (pred(val)) {
    const u: U = val;
    console.log(u);
    return;
  }
  const notU: Exclude<T, U> = val; // Type 'T' is not assignable to type 'Exclude<T, U>'.
  console.log(notU);
}

Expected behavior:

The code should compile, the predicate type-guard sets val to U in the "true" branch, and to Exclude<T, U> in the "false" branch.

Actual behavior:

In the "false" branch val is inferred to be T:

Type 'T' is not assignable to type 'Exclude<T, U>'.

Playground Link: https://bit.ly/2HAtMpF

Related Issues:

@mkulke
Copy link
Contributor Author

mkulke commented Apr 28, 2018

this works:

function doSth<T, U extends T>(pred: Pred<T, U>, val: T): void {
  const antiPred = (val: T): val is Exclude<T, U> => !pred(val);
  if (pred(val)) {
    const u: U = val;
    console.log(u);
    return;
  }
  if (antiPred(val)) {
    const notU: Exclude<T, U> = val;
    console.log(notU);
  }
}

@mkulke
Copy link
Contributor Author

mkulke commented Apr 28, 2018

I made it work by altering val: T to val: U | Exclude<T, U>, which does not feel too wrong. but my intuition says that by stating <T, U extends T>, T would be equivalent to U | Exclude<T, U>. 🤔

type Pred<T, U extends T> = (v: T) => v is U;
function someFn<T, U extends T>(pred: Pred<T, U>, val: U | Exclude<T, U>): void {
  if (pred(val)) {
    const u: U = val;
    console.log(u);
    return;
  }
  const notU: Exclude<T, U> = val;
  console.log(notU);
}

if my intuition is off here, i'll gladly close the issue.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 30, 2018

Related to #22348

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Apr 30, 2018
@RyanCavanaugh RyanCavanaugh added Question An issue which isn't directly actionable in code and removed Suggestion An idea for TypeScript labels Aug 22, 2018
@RyanCavanaugh
Copy link
Member

my intuition says that by stating <T, U extends T>, T would be equivalent to U | Exclude<T, U>

This isn't the case.

@mkulke
Copy link
Contributor Author

mkulke commented Aug 23, 2018

@RyanCavanaugh: alright i close the issue then. if you find the time, you can maybe elaborate. My naive reasoning would be: Given there is "Animal" and "Dog", stating: "Animal" would be be the same as "Dog and every Animal but Dog".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants