Skip to content

Commit

Permalink
Merge pull request #284 from changwoolab/fix/readonly-exhaustive
Browse files Browse the repository at this point in the history
Fixes type `InvertPatternForExcludeInternal` to work with readonly array
  • Loading branch information
gvergnaud authored Sep 23, 2024
2 parents c5778f3 + 5a1e7e0 commit e7fd119
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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 exhaustively match readonly arrays', () => {
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();
});
});

0 comments on commit e7fd119

Please sign in to comment.