Skip to content

given x : T, bool should be a subtype of x is T #19557

New issue

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

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

Already on GitHub? # to your account

Closed
acertain opened this issue Oct 29, 2017 · 3 comments
Closed

given x : T, bool should be a subtype of x is T #19557

acertain opened this issue Oct 29, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@acertain
Copy link

acertain commented Oct 29, 2017

If x : T is already known, any bool implies/is evidence for x is T.

This should help type inference for e.g.

function f<T, R extends T>(x: T[], p: (x: T) => x is R): R | undefined {
  for (const v of x) {
    if(p(v)){return v}
  }
}

// doesn't work
f([2,3,4], (x) => x > 3)
// works
f([2,3,4], (x): x is number => x > 3)

(filter, etc)

@acertain acertain changed the title given x : T, bool should be a subtype of x is T given x : T, bool should be a subtype of x is T Oct 29, 2017
@aluanhaddad
Copy link
Contributor

aluanhaddad commented Oct 30, 2017

This looks like a duplicate of #10734

@ghost
Copy link

ghost commented Oct 30, 2017

There's no type guard for us to infer in the not-working case.
x > 3 isn't correctly of type x is number, because 2 is a number but it would return false. When you explicitly annotate a type predicate we trust you and allow it, but inferring one in this case would be wrong.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 30, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants