Skip to content

Commit ed5e624

Browse files
authored
Merge pull request microsoft#20883 from HerringtonDarkholme/relax-type-constraint
fix microsoft#20018, allow skip constraint when merging interfaces
2 parents 67eabb9 + 303786a commit ed5e624

6 files changed

+179
-7
lines changed

src/compiler/checker.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -22642,9 +22642,11 @@ namespace ts {
2264222642
// type parameter at this position, we report an error.
2264322643
const sourceConstraint = source.constraint && getTypeFromTypeNode(source.constraint);
2264422644
const targetConstraint = getConstraintFromTypeParameter(target);
22645-
if ((sourceConstraint || targetConstraint) &&
22646-
(!sourceConstraint || !targetConstraint || !isTypeIdenticalTo(sourceConstraint, targetConstraint))) {
22647-
return false;
22645+
if (sourceConstraint) {
22646+
// relax check if later interface augmentation has no constraint
22647+
if (!targetConstraint || !isTypeIdenticalTo(sourceConstraint, targetConstraint)) {
22648+
return false;
22649+
}
2264822650
}
2264922651

2265022652
// If the type parameter node has a default and it is not identical to the default

tests/baselines/reference/twoGenericInterfacesWithDifferentConstraints.errors.txt

+29-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDi
44
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(14,15): error TS2428: All declarations of 'B' must have identical type parameters.
55
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(32,22): error TS2428: All declarations of 'A' must have identical type parameters.
66
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(38,22): error TS2428: All declarations of 'A' must have identical type parameters.
7+
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(53,11): error TS2428: All declarations of 'C' must have identical type parameters.
8+
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(57,11): error TS2428: All declarations of 'C' must have identical type parameters.
79

810

9-
==== tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts (6 errors) ====
11+
==== tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts (8 errors) ====
1012
interface A<T extends Date> {
1113
~
1214
!!! error TS2428: All declarations of 'A' must have identical type parameters.
@@ -59,4 +61,29 @@ tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDi
5961
!!! error TS2428: All declarations of 'A' must have identical type parameters.
6062
y: T;
6163
}
62-
}
64+
}
65+
66+
interface B<T extends number> {
67+
u: T;
68+
v: Constraint<T>; // ok
69+
}
70+
71+
interface B<T> { // ok
72+
x: T;
73+
y: Constraint<T>; // ok
74+
}
75+
76+
interface C<T> {
77+
~
78+
!!! error TS2428: All declarations of 'C' must have identical type parameters.
79+
x: T;
80+
}
81+
82+
interface C<T extends number> { // error
83+
~
84+
!!! error TS2428: All declarations of 'C' must have identical type parameters.
85+
y: T;
86+
}
87+
88+
interface Constraint<T extends number> {}
89+

tests/baselines/reference/twoGenericInterfacesWithDifferentConstraints.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ module M3 {
3939
export interface A<T extends Number> { // error
4040
y: T;
4141
}
42-
}
42+
}
43+
44+
interface B<T extends number> {
45+
u: T;
46+
v: Constraint<T>; // ok
47+
}
48+
49+
interface B<T> { // ok
50+
x: T;
51+
y: Constraint<T>; // ok
52+
}
53+
54+
interface C<T> {
55+
x: T;
56+
}
57+
58+
interface C<T extends number> { // error
59+
y: T;
60+
}
61+
62+
interface Constraint<T extends number> {}
63+
4364

4465
//// [twoGenericInterfacesWithDifferentConstraints.js]

tests/baselines/reference/twoGenericInterfacesWithDifferentConstraints.symbols

+51
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,54 @@ module M3 {
9999
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 31, 23), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 37, 23))
100100
}
101101
}
102+
103+
interface B<T extends number> {
104+
>B : Symbol(B, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 40, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 45, 1))
105+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
106+
107+
u: T;
108+
>u : Symbol(B.u, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 31))
109+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
110+
111+
v: Constraint<T>; // ok
112+
>v : Symbol(B.v, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 43, 7))
113+
>Constraint : Symbol(Constraint, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 58, 1))
114+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
115+
}
116+
117+
interface B<T> { // ok
118+
>B : Symbol(B, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 40, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 45, 1))
119+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
120+
121+
x: T;
122+
>x : Symbol(B.x, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 16))
123+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
124+
125+
y: Constraint<T>; // ok
126+
>y : Symbol(B.y, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 48, 7))
127+
>Constraint : Symbol(Constraint, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 58, 1))
128+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
129+
}
130+
131+
interface C<T> {
132+
>C : Symbol(C, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 50, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 54, 1))
133+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
134+
135+
x: T;
136+
>x : Symbol(C.x, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 16))
137+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
138+
}
139+
140+
interface C<T extends number> { // error
141+
>C : Symbol(C, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 50, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 54, 1))
142+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
143+
144+
y: T;
145+
>y : Symbol(C.y, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 31))
146+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
147+
}
148+
149+
interface Constraint<T extends number> {}
150+
>Constraint : Symbol(Constraint, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 58, 1))
151+
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 60, 21))
152+

tests/baselines/reference/twoGenericInterfacesWithDifferentConstraints.types

+51
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,54 @@ module M3 {
9999
>T : T
100100
}
101101
}
102+
103+
interface B<T extends number> {
104+
>B : B<T>
105+
>T : T
106+
107+
u: T;
108+
>u : T
109+
>T : T
110+
111+
v: Constraint<T>; // ok
112+
>v : Constraint<T>
113+
>Constraint : Constraint<T>
114+
>T : T
115+
}
116+
117+
interface B<T> { // ok
118+
>B : B<T>
119+
>T : T
120+
121+
x: T;
122+
>x : T
123+
>T : T
124+
125+
y: Constraint<T>; // ok
126+
>y : Constraint<T>
127+
>Constraint : Constraint<T>
128+
>T : T
129+
}
130+
131+
interface C<T> {
132+
>C : C<T>
133+
>T : T
134+
135+
x: T;
136+
>x : T
137+
>T : T
138+
}
139+
140+
interface C<T extends number> { // error
141+
>C : C<T>
142+
>T : T
143+
144+
y: T;
145+
>y : T
146+
>T : T
147+
}
148+
149+
interface Constraint<T extends number> {}
150+
>Constraint : Constraint<T>
151+
>T : T
152+

tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,24 @@ module M3 {
3838
export interface A<T extends Number> { // error
3939
y: T;
4040
}
41-
}
41+
}
42+
43+
interface B<T extends number> {
44+
u: T;
45+
v: Constraint<T>; // ok
46+
}
47+
48+
interface B<T> { // ok
49+
x: T;
50+
y: Constraint<T>; // ok
51+
}
52+
53+
interface C<T> {
54+
x: T;
55+
}
56+
57+
interface C<T extends number> { // error
58+
y: T;
59+
}
60+
61+
interface Constraint<T extends number> {}

0 commit comments

Comments
 (0)