From ed6b3c72ca13575360bd6e12313bafd601fd81ce Mon Sep 17 00:00:00 2001 From: David Hill Date: Tue, 3 Sep 2024 20:22:54 +0100 Subject: [PATCH 1/4] Add function override, fixes: https://github.com/angus-c/just/issues/584 --- packages/array-partition/index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; From 534be402fd162e94a809cea77d49a4f1f94e8ccd Mon Sep 17 00:00:00 2001 From: David Hill Date: Mon, 11 Nov 2024 22:21:09 +0000 Subject: [PATCH 2/4] Update test, casting of resulting array is no longer necessary --- packages/array-partition/index.tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/array-partition/index.tests.ts b/packages/array-partition/index.tests.ts index 2dd9706c8..ca593babe 100644 --- a/packages/array-partition/index.tests.ts +++ b/packages/array-partition/index.tests.ts @@ -2,7 +2,7 @@ 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 => typeof x == 'string'); const test3: [number[], number[]] = partition([1, 2, 3, 4], x => typeof x == 'string'); const test4: [unknown[], unknown[]] = partition([], n => n > 3); // [[], []] From ae197c2fefb7c7a169d80b433970e3faf0d8b2c7 Mon Sep 17 00:00:00 2001 From: David Hill Date: Mon, 11 Nov 2024 22:42:55 +0000 Subject: [PATCH 3/4] Type function as a type guard --- packages/array-partition/index.tests.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/array-partition/index.tests.ts b/packages/array-partition/index.tests.ts index ca593babe..015ddb39b 100644 --- a/packages/array-partition/index.tests.ts +++ b/packages/array-partition/index.tests.ts @@ -2,7 +2,9 @@ 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'); +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); // [[], []] From 3faee0376118e508e93410564f241358b6786628 Mon Sep 17 00:00:00 2001 From: David Hill Date: Mon, 11 Nov 2024 22:51:01 +0000 Subject: [PATCH 4/4] Add additional test, checking type narrowing --- packages/array-partition/index.tests.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/array-partition/index.tests.ts b/packages/array-partition/index.tests.ts index 015ddb39b..1937ecde5 100644 --- a/packages/array-partition/index.tests.ts +++ b/packages/array-partition/index.tests.ts @@ -7,6 +7,7 @@ const test2: [string[], number[]] = partition(['a', 2, 3, '3'], (x): x is 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 @@ -21,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'; +}