Array functions taking predicates should not have to return boolean. #27496
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
Search Terms
predicate find force boolean
Suggestion
As per #5850
array.prototype.filter
doesn't enforceboolean
as a return type anymore. I want to propose the same behavior for wherever a predicate is needed. This means not onlyfilter
, butfind
,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 hasposition
. 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:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: