Description
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)