Skip to content

Array functions taking predicates should not have to return boolean. #27496

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
4 tasks done
theSdev opened this issue Oct 2, 2018 · 3 comments · Fixed by #31076
Closed
4 tasks done

Array functions taking predicates should not have to return boolean. #27496

theSdev opened this issue Oct 2, 2018 · 3 comments · Fixed by #31076
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@theSdev
Copy link

theSdev commented Oct 2, 2018

Search Terms

predicate find force boolean

Suggestion

As per #5850 array.prototype.filter doesn't enforce boolean as a return type anymore. I want to propose the same behavior for wherever a predicate is needed. This means not only filter, but find, some, every and maybe others. These functions should not enforce boolean as return type of the predicate passed to them.

Use Cases

This will be useful wherever the developer is looking for a result that is not missing some value(s).

Examples

Say I want to find the first parkSpace that has position. This works in TypeScript 3.1.1:
this.selectedParkSpace = this.parkSpaces.filter(o => o.position)[0] || null;

but this doesn't:
this.selectedParkSpace = this.parkSpaces.find(o => o.position) || null;

For the latter, TypeScript complains:

Type 'undefined' is not assignable to type 'boolean'.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Oct 2, 2018
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Oct 2, 2018
@ghost
Copy link

ghost commented Oct 2, 2018

Duplicate of #19456?

@theSdev
Copy link
Author

theSdev commented Oct 2, 2018

@Andy-MS definitely not. #19456 is about making this behavior optional for filter whereas I'm suggesting to expand this behavior to similar cases.

@DanielRosenwasser DanielRosenwasser changed the title Predicates should not have to return boolean. Array functions taking predicates should not have to return boolean. Oct 3, 2018
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@RyanCavanaugh
Copy link
Member

Based on discussion, we'd like to see this with the return type being unknown and see if that breaks anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
2 participants