diff --git a/packages/array-partition/index.d.ts b/packages/array-partition/index.d.ts index d6d8e6597..906466946 100644 --- a/packages/array-partition/index.d.ts +++ b/packages/array-partition/index.d.ts @@ -11,4 +11,6 @@ * partition(['a', 1, 2, 'b'], x => typeof x == 'string'); * // => [['a', 'b'], [1, 2]] */ -export default function partition(arr: T[], resolver: (arg: T) => boolean): [T[], T[]] +declare function partition(arr: T[], resolver: (arg: T) => arg is S): [S[], Exclude[]]; +declare function partition(arr: T[], resolver: (arg: T) => boolean): [T[], T[]] +export default partition; diff --git a/packages/array-partition/index.tests.ts b/packages/array-partition/index.tests.ts index 2dd9706c8..1937ecde5 100644 --- a/packages/array-partition/index.tests.ts +++ b/packages/array-partition/index.tests.ts @@ -2,9 +2,12 @@ import partition from './index'; // OK const test1: [number[], number[]] = partition([1, 5, 2, 4, 3], n => n > 3); -const test2: [string[], number[]] = partition(['a', 2, 3, '3'], x => typeof x == 'string') as [string[], number[]]; //if you know better than typescript +const test2: [string[], number[]] = partition(['a', 2, 3, '3'], (x): x is string => { + return typeof x === 'string'; +}); const test3: [number[], number[]] = partition([1, 2, 3, 4], x => typeof x == 'string'); const test4: [unknown[], unknown[]] = partition([], n => n > 3); // [[], []] +const test5: [boolean[], number[]] = partition([1, true, 2, 3, true], isBoolean); // Not OK // @ts-expect-error @@ -19,3 +22,7 @@ partition(null, n => n > 1); partition(undefined, n => n > 1); // @ts-expect-error partition([1, 5, 2, 4, 3], n => n > 3, "a"); + +function isBoolean(x: unknown): x is boolean { + return typeof x === 'boolean'; +}