Skip to content

Commit ee278d9

Browse files
committed
v5: better support for wrong patterns resulting in the 'any' type
1 parent 1c9abf7 commit ee278d9

5 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/types/ExtractPreciseValue.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { DeepExclude } from './DeepExclude';
1212

1313
export type ExtractPreciseValue<a, b> = unknown extends b
1414
? a
15-
: IsAny<a> extends true
15+
: // inlining IsAny for perf
16+
0 extends 1 & b
17+
? a
18+
: // inlining IsAny for perf
19+
0 extends 1 & a
1620
? b
1721
: b extends readonly []
1822
? []

src/types/FindSelected.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ export type FindSelectionUnion<
9494
p,
9595
// path just serves as an id, to identify different anonymous patterns which have the same type
9696
path extends any[] = []
97-
> = IsAny<i> extends true
97+
// inlining IsAny for perf
98+
> = 0 extends 1 & i
99+
? never
100+
: // inlining IsAny for perf
101+
0 extends 1 & p
98102
? never
99103
: p extends Primitives
100104
? never

src/types/InvertPattern.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
import type { Matcher, Pattern, ToExclude, AnyMatcher } from './Pattern';
1515

1616
type OptionalKeys<p> = ValueOf<{
17-
[k in keyof p]: p[k] extends Matcher<any, any, infer matcherType>
17+
[k in keyof p]: // inlining IsAny for perf
18+
0 extends 1 & p[k]
19+
? never
20+
: p[k] extends Matcher<any, any, infer matcherType>
1821
? matcherType extends 'optional'
1922
? k
2023
: never
@@ -59,12 +62,9 @@ type InvertArrayPattern<
5962
* Since patterns have special wildcard values, we need a way
6063
* to transform a pattern into the type of value it represents
6164
*/
62-
export type InvertPattern<p> = p extends Matcher<
63-
infer input,
64-
infer narrowed,
65-
infer matcherType,
66-
any
67-
>
65+
export type InvertPattern<p> = 0 extends 1 & p
66+
? never
67+
: p extends Matcher<infer input, infer narrowed, infer matcherType, any>
6868
? {
6969
not: ToExclude<InvertPattern<narrowed>>;
7070
select: InvertPattern<narrowed>;

src/types/Match.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export type Match<
3737
with<
3838
const p extends Pattern<i>,
3939
c,
40-
value extends MatchedValue<i, InvertPattern<p>>,
41-
x = InvertPatternForExclude<p, MatchedValue<i, InvertPattern<p>>>
40+
value extends MatchedValue<i, InvertPattern<p>>
4241
>(
4342
/**
4443
* HACK: Using `IsNever<p>` here is a hack to

tests/type-error.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,15 @@ describe('type errors', () => {
115115
// @ts-expect-error
116116
.exhaustive();
117117
});
118+
119+
it("if a pattern is any, the outer expression shouldn't throw a type error", () => {
120+
const anyVar = null as any;
121+
122+
match({ a: 'a' })
123+
.with({ a: anyVar }, (x) => {
124+
type t = Expect<Equal<typeof x, { a: never }>>;
125+
return 'Ok';
126+
})
127+
.otherwise(() => 'ko');
128+
});
118129
});

0 commit comments

Comments
 (0)