Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvment to Array.isArray #151

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/entrypoints/is-array.d.ts
@@ -1,3 +1,3 @@
interface ArrayConstructor {
isArray(arg: any): arg is unknown[];
isArray<T>(arg: true extends TSReset.IsAny<T> ? never : T): arg is true extends TSReset.IsAny<T> ? never : T extends ReadonlyArray<unknown> ? T : Array<T> extends T ? Array<T> : never;
}
Comment on lines 1 to 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I agree with the decision to disallow any, since narrowing a value from any to unknown[] seems like a valid thing to want to do.

Setting that aside, here's another way you could accomplish the same thing:

Suggested change
interface ArrayConstructor {
isArray(arg: any): arg is unknown[];
isArray<T>(arg: true extends TSReset.IsAny<T> ? never : T): arg is true extends TSReset.IsAny<T> ? never : T extends ReadonlyArray<unknown> ? T : Array<T> extends T ? Array<T> : never;
}
interface ArrayConstructor {
isArray<T>(arg: NotAny<T>): arg is Extract<NotAny<T>, readonly unknown[]>;
}
type NotAny<T> = 0 extends 1 & T ? never : T

3 changes: 3 additions & 0 deletions src/entrypoints/utils.d.ts
Expand Up @@ -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<T> = 0 extends 1 & T ? true : false;
}
82 changes: 81 additions & 1 deletion src/tests/is-array.ts
Expand Up @@ -8,6 +8,30 @@ doNotExecute(() => {
}
});

doNotExecute(() => {
const maybeArr = [1, 2, 3] as unknown[];

if (Array.isArray(maybeArr)) {
type tests = [Expect<Equal<typeof maybeArr, unknown[]>>];
}
});

doNotExecute(() => {
const maybeArr = [1, 2, 3] as any;

if (Array.isArray(maybeArr)) {
type tests = [Expect<Equal<typeof maybeArr, any[]>>];
}
});

doNotExecute(() => {
const maybeArr = [1, 2, 3] as any[];

if (Array.isArray(maybeArr)) {
type tests = [Expect<Equal<typeof maybeArr, any[]>>];
}
});

doNotExecute(() => {
const arrOrString = [] as string[] | string;

Expand All @@ -17,9 +41,65 @@ doNotExecute(() => {
});

doNotExecute(() => {
let path: string | string[] = [];
const arrOrString = [] as readonly string[] | string;

if (Array.isArray(arrOrString)) {
type tests = [Expect<Equal<typeof arrOrString, readonly string[]>>];
}
});

doNotExecute(() => {
const arrOrString = [] as readonly string[] | string[] | string;

if (Array.isArray(arrOrString)) {
type tests = [Expect<Equal<typeof arrOrString, readonly string[] | string[]>>];
}
});

doNotExecute(() => {
const arrOrString = [] as string[] | string;

if (Array.isArray(arrOrString)) return;
type tests = [Expect<Equal<typeof arrOrString, string>>];
});

doNotExecute(() => {
const arrOrString = [] as readonly string[] | string;

if (Array.isArray(arrOrString)) return;
type tests = [Expect<Equal<typeof arrOrString, string>>];
});

doNotExecute(() => {
const arrOrString = [] as readonly string[] | string[] | string;

if (Array.isArray(arrOrString)) return;
type tests = [Expect<Equal<typeof arrOrString, string>>];
});

doNotExecute(() => {
let arrOrString = "" as string | [1, 2];

if (Array.isArray(arrOrString)) {
type tests = [Expect<Equal<typeof arrOrString, [1, 2]>>];
}
});

doNotExecute(() => {
let path = [] as string | string[];

const paths = Array.isArray(path) ? path : [path];

type tests = [Expect<Equal<typeof paths, string[]>>];
});

doNotExecute(() => {
function test<T>(value: T) {
type Unarray<T> = T extends Array<infer U> ? U : T;
const inner = <X extends Unarray<T>>(v: X[]) => {};

if (Array.isArray(value)) {
inner(value);
}
}
});