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 2 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: 0 extends 1 & T ? never : T): arg is 0 extends 1 & T ? never : T extends unknown[] | readonly unknown[] ? T : T[] extends T ? T[] : never;

Choose a reason for hiding this comment

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

Not sure if you saw my comment here -- you can simplify this by removing the check for unknown[] entirely.

Suggested change
isArray<T>(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<T>(arg: 0 extends 1 & T ? never : T): arg is 0 extends 1 & T ? never : T extends readonly unknown[] ? T : T[] extends T ? T[] : never;

Copy link
Author

Choose a reason for hiding this comment

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

@ahrjarrett Thanks, I saw your comment but it slipped my mind while I was sitting with the code

}
96 changes: 95 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,79 @@ 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);
}
}
});

doNotExecute(async () => {
function makeArray<Item>(input: Item | ReadonlyArray<Item> | Array<Item>) {
if (Array.isArray(input)) {
return input;
}
return [input];
}

const [first] = makeArray([{ a: "1" }, { a: "2" }, { a: "3" }] as const);

// No error!
first.a;
});