From 802a26f7c9a00e306bcb83aaf0228e497ae97d78 Mon Sep 17 00:00:00 2001 From: Neef Rehman Date: Sun, 19 Nov 2023 12:44:32 +0000 Subject: [PATCH] fix(types): change `any[]` to `unknown[]`, if creating a matrix of dynamic size BREAKING CHANGE --- README.md | 4 ++-- src/__tests__/types.test.ts | 2 +- src/types.ts | 7 +++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 120bcc8..12526ed 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,10 @@ threeDNumberArray[2][1] = 10; // 🚨 error: Type 'number' is not assign threeDNumberArray[2][1][2][0] = 10; // 🚨 error: Property '0' does not exist on type 'Number' ``` -Please be aware that—when using this function with a primitive instead of a literal type, such as when creating matrices of dynamic size—the exact return type can't be determined and will instead resolve to `any[]`. +Please be aware that when using this function with a primitive instead of a literal type—such as when creating matrices of dynamic size—the exact return type cannot be determined and will instead resolve to `unknown[]`. This ensures an array of at least one dimension is returned, but is unable to provide any more safety than that. You must type the resulting matrix yourself, if you are able to provide more information than the compiler can infer. If not, then an explicit return type of `any[]` will provide the most 'relaxed' typing experience. ```ts -const createDynamicMatrix = (dimensions: number[]) => makeMatrix(dimensions, 0); // return type of any[] +const createDynamicMatrix = (dimensions: number[]) => makeMatrix(dimensions, 0); // return type of unknown[] ``` ## Example diff --git a/src/__tests__/types.test.ts b/src/__tests__/types.test.ts index 53d1215..5640578 100644 --- a/src/__tests__/types.test.ts +++ b/src/__tests__/types.test.ts @@ -7,7 +7,7 @@ describe("assert types", () => { expectType(makeMatrix([1, 1, 1, 1], "hello")); expectType(makeMatrix([4, 5], ([a]) => a)); expectType(makeMatrix([4], v => v[0].toString())); - expectType(makeMatrix([] as number[])); // eslint-disable-line @typescript-eslint/no-explicit-any + expectType(makeMatrix([] as number[])); expectType<(0 | 1)[]>( makeMatrix([1], () => (Math.random() > 0.5 ? (0 as const) : (1 as const))) diff --git a/src/types.ts b/src/types.ts index 74fbe43..d741539 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,16 +9,15 @@ export type Matrix = Brand<_Matrix>; * @remarks * When a `_Matrix` is created without knowing it's exact size or number of dimensions, such * as when creating one dynamically, it's impossible recursively construct a type for it. - * Instead, we short circuit the type to resolve to `any[]`, to avoid the typescript compiler - * throwing a `type instantiation is excessively deep...` error. This ensures an array of at - * least one dimension is returned, but unfortunately can't provide more safety than that. + * Instead, we short circuit the type to resolve to `unknown[]`. This ensures an array of at + * least one dimension is returned, but is unable to provide any more safety than that. */ type _Matrix< D extends number, T = unknown, RecursionCountArray extends number[] = [], > = number extends D - ? any[] // eslint-disable-line @typescript-eslint/no-explicit-any + ? unknown[] : D extends RecursionCountArray["length"] ? T : _Matrix;