Skip to content

Support typeof typeguards for .every() #23799

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
TheLarkInn opened this issue May 1, 2018 · 2 comments
Closed

Support typeof typeguards for .every() #23799

TheLarkInn opened this issue May 1, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@TheLarkInn
Copy link
Member

TypeScript Version: 2.9.0-dev.20180426

Search Terms:
predicate callback .every typeguards typeof
Code

interface Foo {
    id: string|number
}

function doSome(stuff: Foo[]): false|[number, number] {
    if (stuff.every(s => typeof s.id === "number")) {
        var minId = -Infinity;
        var maxId = Infinity;

        for (let stf of stuff) {
            if (maxId < stf.id) maxId = stf.id;
            if (minId > stf.id) minId = stf.id;
        }
        return [minId, maxId];
    }
    return false;
}

Expected behavior:
No type error

Actual behavior:
Error:

Type 'string | number' is not assignable to type 'number'.
  Type 'string' is not assignable to type 'number'.

Playground Link:
https://www.typescriptlang.org/play/#src=interface%20Foo%20%7B%0D%0A%20%20%20%20id%3A%20string%7Cnumber%0D%0A%7D%0D%0A%0D%0Afunction%20doSome(stuff%3A%20Foo%5B%5D)%3A%20false%7C%5Bnumber%2C%20number%5D%20%7B%0D%0A%20%20%20%20if%20(stuff.every(s%20%3D%3E%20typeof%20s.id%20%3D%3D%3D%20%22number%22))%20%7B%0D%0A%20%20%20%20%20%20%20%20var%20minId%20%3D%20-Infinity%3B%0D%0A%20%20%20%20%20%20%20%20var%20maxId%20%3D%20Infinity%3B%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%0D%0A%20%20%20%20%20%20%20%20for%20(let%20stf%20of%20stuff)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(maxId%20%3C%20stf.id)%20maxId%20%3D%20stf.id%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(minId%20%3E%20stf.id)%20minId%20%3D%20stf.id%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20return%20%5BminId%2C%20maxId%5D%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20return%20false%3B%0D%0A%7D

Related Issues:

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 1, 2018

Duplicate of #14963 😢

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label May 1, 2018
@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.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants