Skip to content

Typeguard-friendly Array#every #38262

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
wants to merge 2 commits into from
Closed

Typeguard-friendly Array#every #38262

wants to merge 2 commits into from

Conversation

fazouane-marouane
Copy link

@fazouane-marouane fazouane-marouane commented Apr 30, 2020

Fixes # I'm not aware of any issue

This PR tries to address the following. However, it may not play well with mutations.

function is_number(v: unknown): v is number {
  return typeof v === "number";
}

function weird_sum(collection: Array<string | number>) {
  if (collection.every(is_number)) {
    // currently, typescript raises an error here
    const sum: number = collection.reduce((acc, v) => acc + v);
    return sum;
  }
  return 0;
}

weird_sum([5, "abc", 42]);

Please verify that:

  • There is an associated issue in the Backlog milestone (required)
  • Code is up-to-date with the master branch
  • You've successfully run gulp runtests locally
  • There are new or updated unit tests validating the change

@typescript-bot
Copy link
Collaborator

It looks like you've sent a pull request to update our 'lib' files. These files aren't meant to be edited by hand, as they consist of last-known good states of the compiler and are generated from 'src'. Unless this is necessary, consider closing the pull request and sending a separate PR to update 'src'.

@typescript-bot typescript-bot added the lib update PR modifies files in the `lib` folder label Apr 30, 2020
@msftclas
Copy link

msftclas commented Apr 30, 2020

CLA assistant check
All CLA requirements met.

@ikokostya
Copy link
Contributor

Duplicate of #38200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib update PR modifies files in the `lib` folder
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants