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

Add Array.isArray improvement #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions readme.md
Expand Up @@ -256,6 +256,43 @@ const validate = (input: unknown) => {
};
```

### Make `Array.isArray` correctly narrow with `ReadonlyArray<Item>`

```ts
import "@total-typescript/ts-reset/array-includes";
```

This rule improves on TypeScript's default `Array.isArray` behaviour. Without this rule enabled, TypeScript will not include `ReadonlyArray<Item>` when narrowing Array types.
phenomnomnominal marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

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

Also I weakly suggest using American standard spelling, though I suspect Mr. Pocock will prefer yours. ;)

Suggested change
This rule improves on TypeScript's default `Array.isArray` behaviour. Without this rule enabled, TypeScript will not include `ReadonlyArray<Item>` when narrowing Array types.
This rule improves on TypeScript's default `Array.isArray` behavior. With this rule enabled, TypeScript will now include `ReadonlyArray<Item>` when narrowing Array types.

Copy link
Author

Choose a reason for hiding this comment

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

🤓 Commonwealth power!


```ts
// BEFORE
function makeArray(input: string | ReadonlyArray<string> | Array<string>) {
if (Array.isArray(input)) {
return input;
}
return [input];
}

type Result = ReturnType<typeof makeArray>; // (string | readonly string[])[]
```

This is almost definitely not what the user intended! While it works correctly at run-time (since JS has no concept of ReadonlyArray), the inferred types are very incorrect.

```ts
// AFTER
import "@total-typescript/ts-reset/array-includes";

function makeArray(input: string | ReadonlyArray<string> | Array<string>) {
// Array.isArray now
if (Array.isArray(input)) {
return input;
}
return [input];
}

type Result = ReturnType<typeof makeArray>; // string[]
```

## Rules we won't add

### `Object.keys`/`Object.entries`
Expand Down
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(arg: any): arg is ReadonlyArray<unknown> | Array<unknown>;
}
18 changes: 17 additions & 1 deletion src/tests/is-array.ts
Expand Up @@ -4,7 +4,9 @@ doNotExecute(() => {
const maybeArr = [1, 2, 3] as unknown;

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

Expand All @@ -15,3 +17,17 @@ doNotExecute(() => {
type tests = [Expect<Equal<typeof arrOrString, string[]>>];
}
});

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;
});