Skip to content

Commit 3902ef7

Browse files
authored
Merge pull request #30917 from Microsoft/fixIntersectionUnionConstraint
Fix checking of intersection with union constraint
2 parents 4fee628 + f3fd27c commit 3902ef7

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7598,11 +7598,6 @@ namespace ts {
75987598
constraint = getConstraintOfType(constraint);
75997599
}
76007600
if (constraint) {
7601-
// A constraint that isn't a union type implies that the final type would be a non-union
7602-
// type as well. Since non-union constraints are of no interest, we can exit here.
7603-
if (!(constraint.flags & TypeFlags.Union)) {
7604-
return undefined;
7605-
}
76067601
constraints = append(constraints, constraint);
76077602
}
76087603
}

tests/baselines/reference/intersectionWithUnionConstraint.errors.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@ tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts(12
8080

8181
type UnexpectedError<T extends PropertyKey> = T
8282
type NoErrorHere<T extends PropertyKey> = T
83+
84+
// Repro from #30331
85+
86+
type a<T> = T extends Array<infer U> ? U : never;
87+
type b<T> = { [K in a<T> & keyof T ]: 42 };
8388

tests/baselines/reference/intersectionWithUnionConstraint.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ type Example<T, U> = { [K in keyof T]: K extends keyof U ? UnexpectedError<K> :
3333

3434
type UnexpectedError<T extends PropertyKey> = T
3535
type NoErrorHere<T extends PropertyKey> = T
36+
37+
// Repro from #30331
38+
39+
type a<T> = T extends Array<infer U> ? U : never;
40+
type b<T> = { [K in a<T> & keyof T ]: 42 };
3641

3742

3843
//// [intersectionWithUnionConstraint.js]

tests/baselines/reference/intersectionWithUnionConstraint.symbols

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,21 @@ type NoErrorHere<T extends PropertyKey> = T
112112
>PropertyKey : Symbol(PropertyKey, Decl(lib.es5.d.ts, --, --))
113113
>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 33, 17))
114114

115+
// Repro from #30331
116+
117+
type a<T> = T extends Array<infer U> ? U : never;
118+
>a : Symbol(a, Decl(intersectionWithUnionConstraint.ts, 33, 43))
119+
>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 37, 7))
120+
>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 37, 7))
121+
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
122+
>U : Symbol(U, Decl(intersectionWithUnionConstraint.ts, 37, 33))
123+
>U : Symbol(U, Decl(intersectionWithUnionConstraint.ts, 37, 33))
124+
125+
type b<T> = { [K in a<T> & keyof T ]: 42 };
126+
>b : Symbol(b, Decl(intersectionWithUnionConstraint.ts, 37, 49))
127+
>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 38, 7))
128+
>K : Symbol(K, Decl(intersectionWithUnionConstraint.ts, 38, 15))
129+
>a : Symbol(a, Decl(intersectionWithUnionConstraint.ts, 33, 43))
130+
>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 38, 7))
131+
>T : Symbol(T, Decl(intersectionWithUnionConstraint.ts, 38, 7))
132+

tests/baselines/reference/intersectionWithUnionConstraint.types

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ type UnexpectedError<T extends PropertyKey> = T
8484
type NoErrorHere<T extends PropertyKey> = T
8585
>NoErrorHere : T
8686

87+
// Repro from #30331
88+
89+
type a<T> = T extends Array<infer U> ? U : never;
90+
>a : a<T>
91+
92+
type b<T> = { [K in a<T> & keyof T ]: 42 };
93+
>b : b<T>
94+

tests/cases/conformance/types/intersection/intersectionWithUnionConstraint.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ type Example<T, U> = { [K in keyof T]: K extends keyof U ? UnexpectedError<K> :
3434

3535
type UnexpectedError<T extends PropertyKey> = T
3636
type NoErrorHere<T extends PropertyKey> = T
37+
38+
// Repro from #30331
39+
40+
type a<T> = T extends Array<infer U> ? U : never;
41+
type b<T> = { [K in a<T> & keyof T ]: 42 };

0 commit comments

Comments
 (0)