From ab9d475159435d0a4fb11ccb8a069242aa50834b Mon Sep 17 00:00:00 2001 From: Mickemouse0 <31856324+Mickemouse0@users.noreply.github.com> Date: Sun, 18 Jun 2023 00:55:13 +0200 Subject: [PATCH 1/5] Fixes to isArray --- src/entrypoints/is-array.d.ts | 2 +- src/tests/is-array.ts | 96 ++++++++++++++++++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/entrypoints/is-array.d.ts b/src/entrypoints/is-array.d.ts index 199bc66..583777e 100644 --- a/src/entrypoints/is-array.d.ts +++ b/src/entrypoints/is-array.d.ts @@ -1,3 +1,3 @@ interface ArrayConstructor { - isArray(arg: any): arg is unknown[]; + isArray(arg: 0 extends 1 & T ? never : T): arg is 0 extends 1 & T ? never : T extends Array | ReadonlyArray ? T : T[] extends T ? T[] : never; } diff --git a/src/tests/is-array.ts b/src/tests/is-array.ts index daaa12c..2333c03 100644 --- a/src/tests/is-array.ts +++ b/src/tests/is-array.ts @@ -8,6 +8,30 @@ doNotExecute(() => { } }); +doNotExecute(() => { + const maybeArr = [1, 2, 3] as unknown[]; + + if (Array.isArray(maybeArr)) { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const maybeArr = [1, 2, 3] as any; + + if (Array.isArray(maybeArr)) { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const maybeArr = [1, 2, 3] as any[]; + + if (Array.isArray(maybeArr)) { + type tests = [Expect>]; + } +}); + doNotExecute(() => { const arrOrString = [] as string[] | string; @@ -17,9 +41,79 @@ doNotExecute(() => { }); doNotExecute(() => { - let path: string | string[] = []; + const arrOrString = [] as readonly string[] | string; + + if (Array.isArray(arrOrString)) { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const arrOrString = [] as readonly string[] | string[] | string; + + if (Array.isArray(arrOrString)) { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const arrOrString = [] as string[] | string; + + if (Array.isArray(arrOrString)) return; + type tests = [Expect>]; +}); + +doNotExecute(() => { + const arrOrString = [] as readonly string[] | string; + + if (Array.isArray(arrOrString)) return; + type tests = [Expect>]; +}); + +doNotExecute(() => { + const arrOrString = [] as readonly string[] | string[] | string; + + if (Array.isArray(arrOrString)) return; + type tests = [Expect>]; +}); + +doNotExecute(() => { + let arrOrString = "" as string | [1, 2]; + + if (Array.isArray(arrOrString)) { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + let path = [] as string | string[]; const paths = Array.isArray(path) ? path : [path]; type tests = [Expect>]; }); + +doNotExecute(() => { + function test(value: T) { + type Unarray = T extends Array ? U : T; + const inner = >(v: X[]) => {}; + + if (Array.isArray(value)) { + inner(value); + } + } +}); + +doNotExecute(async () => { + function makeArray(input: Item | ReadonlyArray | Array) { + if (Array.isArray(input)) { + return input; + } + return [input]; + } + + const [first] = makeArray([{ a: "1" }, { a: "2" }, { a: "3" }] as const); + + // No error! + first.a; +}); From d2918bb78b48874bfa27ec9b692c372f51b36945 Mon Sep 17 00:00:00 2001 From: Mickemouse0 <31856324+Mickemouse0@users.noreply.github.com> Date: Sun, 18 Jun 2023 01:05:23 +0200 Subject: [PATCH 2/5] Update is-array.d.ts --- src/entrypoints/is-array.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entrypoints/is-array.d.ts b/src/entrypoints/is-array.d.ts index 583777e..8484b86 100644 --- a/src/entrypoints/is-array.d.ts +++ b/src/entrypoints/is-array.d.ts @@ -1,3 +1,3 @@ interface ArrayConstructor { - isArray(arg: 0 extends 1 & T ? never : T): arg is 0 extends 1 & T ? never : T extends Array | ReadonlyArray ? T : T[] extends T ? T[] : never; + isArray(arg: 0 extends 1 & T ? never : T): arg is 0 extends 1 & T ? never : T extends unknown[] | readonly unknown[] ? T : T[] extends T ? T[] : never; } From 710e603101b0b01ecae57c204f9aa5825b637955 Mon Sep 17 00:00:00 2001 From: Mickemouse0 <31856324+Mickemouse0@users.noreply.github.com> Date: Tue, 20 Jun 2023 07:10:38 +0200 Subject: [PATCH 3/5] Improve readability --- src/entrypoints/is-array.d.ts | 2 +- src/entrypoints/utils.d.ts | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/entrypoints/is-array.d.ts b/src/entrypoints/is-array.d.ts index 8484b86..6b4b814 100644 --- a/src/entrypoints/is-array.d.ts +++ b/src/entrypoints/is-array.d.ts @@ -1,3 +1,3 @@ interface ArrayConstructor { - isArray(arg: 0 extends 1 & T ? never : T): arg is 0 extends 1 & T ? never : T extends unknown[] | readonly unknown[] ? T : T[] extends T ? T[] : never; + isArray(arg: true extends TSReset.IsAny ? never : T): arg is true extends TSReset.IsAny ? never : T extends ReadonlyArray ? T : Array extends T ? Array : never; } diff --git a/src/entrypoints/utils.d.ts b/src/entrypoints/utils.d.ts index 00171cb..4d7c6be 100644 --- a/src/entrypoints/utils.d.ts +++ b/src/entrypoints/utils.d.ts @@ -14,4 +14,7 @@ declare namespace TSReset { : T extends symbol ? symbol : T; + + // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 + type IsAny = 0 extends 1 & T ? true : false; } From db19046f4e69ed55a52e5ddb2a47070ae6b7d3c7 Mon Sep 17 00:00:00 2001 From: Mickemouse0 <31856324+Mickemouse0@users.noreply.github.com> Date: Tue, 20 Jun 2023 07:12:37 +0200 Subject: [PATCH 4/5] Fix indentation --- src/entrypoints/utils.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/entrypoints/utils.d.ts b/src/entrypoints/utils.d.ts index 4d7c6be..e1dc76c 100644 --- a/src/entrypoints/utils.d.ts +++ b/src/entrypoints/utils.d.ts @@ -15,6 +15,6 @@ declare namespace TSReset { ? symbol : T; - // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 + // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 type IsAny = 0 extends 1 & T ? true : false; } From 4be3b4f78716d649bc287a81f4ce8d92429abc46 Mon Sep 17 00:00:00 2001 From: Mickemouse0 <31856324+Mickemouse0@users.noreply.github.com> Date: Fri, 23 Jun 2023 11:42:47 +0200 Subject: [PATCH 5/5] Remove invalid test --- src/tests/is-array.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/tests/is-array.ts b/src/tests/is-array.ts index 2333c03..e8ec25c 100644 --- a/src/tests/is-array.ts +++ b/src/tests/is-array.ts @@ -103,17 +103,3 @@ doNotExecute(() => { } } }); - -doNotExecute(async () => { - function makeArray(input: Item | ReadonlyArray | Array) { - if (Array.isArray(input)) { - return input; - } - return [input]; - } - - const [first] = makeArray([{ a: "1" }, { a: "2" }, { a: "3" }] as const); - - // No error! - first.a; -});