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

Fixes type InvertPatternForExcludeInternal to work with readonly array #284

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/types/InvertPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ type InvertPatternForExcludeInternal<p, i, empty = never> =
? {
select: InvertPatternForExcludeInternal<subpattern, i, empty>;
array: i extends readonly (infer ii)[]
? InvertPatternForExcludeInternal<subpattern, ii, empty>[]
? MaybeAddReadonly<
InvertPatternForExcludeInternal<subpattern, ii, empty>[],
IsReadonlyArray<i>
>
: empty;
map: subpattern extends [infer pk, infer pv]
? i extends Map<infer ik, infer iv>
Expand Down
11 changes: 11 additions & 0 deletions tests/exhaustive-match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,4 +979,15 @@ describe('exhaustive()', () => {
expect(withoutTypo({ sex: 'b', age: 'c' })).toBe(1);
});
});

it('issue #271: P.array should support readonly arrays as its input', () => {
gvergnaud marked this conversation as resolved.
Show resolved Hide resolved
type Input = string | Date | readonly string[];
const input = ['a', 'b', 'c'] as Input;

const output = match(input)
.with(P.array(P.string), (value) => 1)
.with(P.string, (value) => 2)
.with(P.instanceOf(Date), (value) => 3)
.exhaustive();
});
});