Skip to content

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

Closed
@theSdev

Description

@theSdev

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions