typedash
Version:
modern, type-safe collection of utility functions
30 lines (27 loc) • 1.34 kB
text/typescript
import { IsLiteral } from 'type-fest';
/**
* Gets the difference between two arrays.
* @param array1 The first array.
* @param array2 The second array.
* @param comparator A function that determines whether two items are equal.
* @returns An array with the difference between the two arrays - items that are in the first array but not in the second.
* @example
* ```ts
* difference([1, 2, 3], [2, 3, 4]) // [2, 3]
* ```
*/
declare function difference<const T, const S extends T>(array1: readonly T[], array2: readonly S[], comparator?: Comparator<T, S>): Array<IsLiteral<T> | IsLiteral<S> extends true ? Exclude<T, S> : T>;
/**
* Gets the difference between two arrays.
* @param array1 The first array.
* @param array2 The second array.
* @param comparator A function that determines whether two items are equal.
* @returns An array with the difference between the two arrays - items that are in the first array but not in the second.
* @example
* ```ts
* difference([1, 2, 3], [2, 3, 4]) // [2, 3]
* ```
*/
declare function difference<T, S extends T>(array1: readonly T[], array2: readonly S[], comparator?: Comparator<T, S>): Array<IsLiteral<T> | IsLiteral<S> extends true ? Exclude<T, S> : T>;
type Comparator<T, S extends T> = ((a: T, b: S) => a is Exclude<T, S>) | ((a: T, b: S) => boolean);
export { difference };