Skip to content

Commit

Permalink
Merge pull request #201 from none23/fix-const-array-filter
Browse files Browse the repository at this point in the history
fix: filter error with `as const` unions
  • Loading branch information
mattpocock committed Aug 24, 2024
2 parents 8b00ac3 + 53cee4f commit 7fd11d3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/metal-moose-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@total-typescript/ts-reset": patch
---

@author: none23

Fixed a bug where running .filter on a union of arrays would not work.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v3
with:
node-version: 20.x
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"prettier": "^3.2.5",
"tsx": "^3.14.0",
"turbo": "^1.13.3",
"typescript": "^5.4.5"
"typescript": "^5.5.4"
},
"prettier": {
"arrowParens": "always",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/entrypoints/filter-boolean.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/// <reference path="utils.d.ts" />

interface Array<T> {
filter(predicate: BooleanConstructor, thisArg?: any): TSReset.NonFalsy<T>[];
filter<S extends T>(
predicate: BooleanConstructor,
thisArg?: any,
): TSReset.NonFalsy<S>[];
}

interface ReadonlyArray<T> {
filter(predicate: BooleanConstructor, thisArg?: any): TSReset.NonFalsy<T>[];
filter<S extends T>(
predicate: BooleanConstructor,
thisArg?: any,
): TSReset.NonFalsy<S>[];
}
12 changes: 12 additions & 0 deletions src/tests/array-index-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ doNotExecute(async () => {
);
});

doNotExecute(() => {
const arr: string[] | number[] = {} as any;

const result = arr.indexOf("abc");
});

// lastIndexOf

doNotExecute(async () => {
Expand Down Expand Up @@ -131,3 +137,9 @@ doNotExecute(async () => {
true,
);
});

doNotExecute(() => {
const arr: string[] | number[] = {} as any;

const result = arr.lastIndexOf("abc");
});
8 changes: 8 additions & 0 deletions src/tests/filter-boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ doNotExecute(() => {

type tests = [Expect<Equal<typeof result, never[]>>];
});

doNotExecute(() => {
const arr: string[] | number[] = {} as any;

const result = arr.filter((x) => typeof x === "string");

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

0 comments on commit 7fd11d3

Please sign in to comment.