Skip to content

Commit

Permalink
Merge pull request #20883 from HerringtonDarkholme/relax-type-constraint
Browse files Browse the repository at this point in the history
fix #20018, allow skip constraint when merging interfaces
  • Loading branch information
rbuckton authored Jan 5, 2018
2 parents 67eabb9 + 303786a commit ed5e624
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22642,9 +22642,11 @@ namespace ts {
// type parameter at this position, we report an error.
const sourceConstraint = source.constraint && getTypeFromTypeNode(source.constraint);
const targetConstraint = getConstraintFromTypeParameter(target);
if ((sourceConstraint || targetConstraint) &&
(!sourceConstraint || !targetConstraint || !isTypeIdenticalTo(sourceConstraint, targetConstraint))) {
return false;
if (sourceConstraint) {
// relax check if later interface augmentation has no constraint
if (!targetConstraint || !isTypeIdenticalTo(sourceConstraint, targetConstraint)) {
return false;
}
}

// If the type parameter node has a default and it is not identical to the default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDi
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(14,15): error TS2428: All declarations of 'B' must have identical type parameters.
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(32,22): error TS2428: All declarations of 'A' must have identical type parameters.
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(38,22): error TS2428: All declarations of 'A' must have identical type parameters.
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(53,11): error TS2428: All declarations of 'C' must have identical type parameters.
tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts(57,11): error TS2428: All declarations of 'C' must have identical type parameters.


==== tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts (6 errors) ====
==== tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts (8 errors) ====
interface A<T extends Date> {
~
!!! error TS2428: All declarations of 'A' must have identical type parameters.
Expand Down Expand Up @@ -59,4 +61,29 @@ tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDi
!!! error TS2428: All declarations of 'A' must have identical type parameters.
y: T;
}
}
}

interface B<T extends number> {
u: T;
v: Constraint<T>; // ok
}

interface B<T> { // ok
x: T;
y: Constraint<T>; // ok
}

interface C<T> {
~
!!! error TS2428: All declarations of 'C' must have identical type parameters.
x: T;
}

interface C<T extends number> { // error
~
!!! error TS2428: All declarations of 'C' must have identical type parameters.
y: T;
}

interface Constraint<T extends number> {}

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ module M3 {
export interface A<T extends Number> { // error
y: T;
}
}
}

interface B<T extends number> {
u: T;
v: Constraint<T>; // ok
}

interface B<T> { // ok
x: T;
y: Constraint<T>; // ok
}

interface C<T> {
x: T;
}

interface C<T extends number> { // error
y: T;
}

interface Constraint<T extends number> {}


//// [twoGenericInterfacesWithDifferentConstraints.js]
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,54 @@ module M3 {
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 31, 23), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 37, 23))
}
}

interface B<T extends number> {
>B : Symbol(B, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 40, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 45, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))

u: T;
>u : Symbol(B.u, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 31))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))

v: Constraint<T>; // ok
>v : Symbol(B.v, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 43, 7))
>Constraint : Symbol(Constraint, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 58, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
}

interface B<T> { // ok
>B : Symbol(B, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 40, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 45, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))

x: T;
>x : Symbol(B.x, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 16))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))

y: Constraint<T>; // ok
>y : Symbol(B.y, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 48, 7))
>Constraint : Symbol(Constraint, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 58, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 42, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 47, 12))
}

interface C<T> {
>C : Symbol(C, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 50, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 54, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))

x: T;
>x : Symbol(C.x, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 16))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
}

interface C<T extends number> { // error
>C : Symbol(C, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 50, 1), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 54, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))

y: T;
>y : Symbol(C.y, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 31))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
}

interface Constraint<T extends number> {}
>Constraint : Symbol(Constraint, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 58, 1))
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 60, 21))

Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,54 @@ module M3 {
>T : T
}
}

interface B<T extends number> {
>B : B<T>
>T : T

u: T;
>u : T
>T : T

v: Constraint<T>; // ok
>v : Constraint<T>
>Constraint : Constraint<T>
>T : T
}

interface B<T> { // ok
>B : B<T>
>T : T

x: T;
>x : T
>T : T

y: Constraint<T>; // ok
>y : Constraint<T>
>Constraint : Constraint<T>
>T : T
}

interface C<T> {
>C : C<T>
>T : T

x: T;
>x : T
>T : T
}

interface C<T extends number> { // error
>C : C<T>
>T : T

y: T;
>y : T
>T : T
}

interface Constraint<T extends number> {}
>Constraint : Constraint<T>
>T : T

Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,24 @@ module M3 {
export interface A<T extends Number> { // error
y: T;
}
}
}

interface B<T extends number> {
u: T;
v: Constraint<T>; // ok
}

interface B<T> { // ok
x: T;
y: Constraint<T>; // ok
}

interface C<T> {
x: T;
}

interface C<T extends number> { // error
y: T;
}

interface Constraint<T extends number> {}

0 comments on commit ed5e624

Please sign in to comment.