Dev Helpers
← Back

Partition by a typed predicate

Partition by a typed predicate using TypeScript.

Open this example in the interactive library

Source code

const words = ["build", "ship", "test"];
console.log(
  (<T,>(items: T[], fn: (v: T) => boolean): [T[], T[]] => [
    items.filter(fn),
    items.filter((v) => !fn(v)),
  ])(words, (w) => w.startsWith("s")),
);

Sample output

[ [ 'ship' ], [ 'build', 'test' ] ]