// Created on savesnippets.com · https://savesnippets.com/Jl2BAWifB8zjXw export function partition( items: readonly T[], pred: (x: T) => x is S ): [S[], Exclude[]]; export function partition( items: readonly T[], pred: (x: T) => boolean ): [T[], T[]]; export function partition(items: readonly T[], pred: (x: T) => boolean): [T[], T[]] { const pass: T[] = [], fail: T[] = []; for (const x of items) (pred(x) ? pass : fail).push(x); return [pass, fail]; } const isString = (x: unknown): x is string => typeof x === 'string'; const [strs, others] = partition([1, 'a', 2, 'b', null], isString); // strs: string[] // others: (number | null)[] ← narrowed via the type guard