Skip to content

Commit e7fd119

Browse files
authored
Merge pull request #284 from changwoolab/fix/readonly-exhaustive
Fixes type `InvertPatternForExcludeInternal` to work with readonly array
2 parents c5778f3 + 5a1e7e0 commit e7fd119

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/types/InvertPattern.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,10 @@ type InvertPatternForExcludeInternal<p, i, empty = never> =
318318
? {
319319
select: InvertPatternForExcludeInternal<subpattern, i, empty>;
320320
array: i extends readonly (infer ii)[]
321-
? InvertPatternForExcludeInternal<subpattern, ii, empty>[]
321+
? MaybeAddReadonly<
322+
InvertPatternForExcludeInternal<subpattern, ii, empty>[],
323+
IsReadonlyArray<i>
324+
>
322325
: empty;
323326
map: subpattern extends [infer pk, infer pv]
324327
? i extends Map<infer ik, infer iv>

tests/exhaustive-match.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -979,4 +979,15 @@ describe('exhaustive()', () => {
979979
expect(withoutTypo({ sex: 'b', age: 'c' })).toBe(1);
980980
});
981981
});
982+
983+
it('issue #271: P.array should exhaustively match readonly arrays', () => {
984+
type Input = string | Date | readonly string[];
985+
const input = ['a', 'b', 'c'] as Input;
986+
987+
const output = match(input)
988+
.with(P.array(P.string), (value) => 1)
989+
.with(P.string, (value) => 2)
990+
.with(P.instanceOf(Date), (value) => 3)
991+
.exhaustive();
992+
});
982993
});

0 commit comments

Comments
 (0)