Merge pull request #20883 from HerringtonDarkholme/relax-type-constraint

fix #20018, allow skip constraint when merging interfaces
This commit is contained in:
Ron Buckton 2018-01-04 17:09:45 -08:00 committed by GitHub
commit ed5e62442f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 179 additions and 7 deletions

View file

@ -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

View file

@ -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.
@ -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> {}

View file

@ -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]

View file

@ -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))

View file

@ -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

View file

@ -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> {}