Skip to content

Commit

Permalink
getConstraintDeclaration gets the first declaration with a constraint… (
Browse files Browse the repository at this point in the history
#33426)

* getConstraintDeclaration gets the first declaration with a constraint, rather than just the first declaration

* Add type annotation

* Update comment
  • Loading branch information
weswigham authored Sep 18, 2019
1 parent 683e281 commit 5e06bea
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 20 deletions.
14 changes: 6 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8971,9 +8971,8 @@ namespace ts {
return undefined;
}

function getConstraintDeclaration(type: TypeParameter) {
const decl = type.symbol && getDeclarationOfKind<TypeParameterDeclaration>(type.symbol, SyntaxKind.TypeParameter);
return decl && getEffectiveConstraintOfTypeParameter(decl);
function getConstraintDeclaration(type: TypeParameter): TypeNode | undefined {
return mapDefined(filter(type.symbol && type.symbol.declarations, isTypeParameterDeclaration), getEffectiveConstraintOfTypeParameter)[0];
}

function getInferredTypeParameterConstraint(typeParameter: TypeParameter) {
Expand Down Expand Up @@ -29242,11 +29241,10 @@ namespace ts {
const constraint = getEffectiveConstraintOfTypeParameter(source);
const sourceConstraint = constraint && getTypeFromTypeNode(constraint);
const targetConstraint = getConstraintOfTypeParameter(target);
if (sourceConstraint) {
// relax check if later interface augmentation has no constraint
if (!targetConstraint || !isTypeIdenticalTo(sourceConstraint, targetConstraint)) {
return false;
}
// relax check if later interface augmentation has no constraint, it's more broad and is OK to merge with
// a more constrained interface (this could be generalized to a full heirarchy check, but that's maybe overkill)
if (sourceConstraint && 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
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/interfaceMergedUnconstrainedNoErrorIrrespectiveOfOrder.ts] ////

//// [working.ts]
// minmal samples from #33395
export namespace ns {
interface Function<T extends (...args: any) => any> {
throttle(): Function<T>;
}
interface Function<T> {
unary(): Function<() => ReturnType<T>>;
}
}
//// [regression.ts]
export namespace ns {
interface Function<T> {
unary(): Function<() => ReturnType<T>>;
}
interface Function<T extends (...args: any) => any> {
throttle(): Function<T>;
}
}

//// [working.js]
"use strict";
exports.__esModule = true;
//// [regression.js]
"use strict";
exports.__esModule = true;
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/working.ts ===
// minmal samples from #33395
export namespace ns {
>ns : Symbol(ns, Decl(working.ts, 0, 0))

interface Function<T extends (...args: any) => any> {
>Function : Symbol(Function, Decl(working.ts, 1, 21), Decl(working.ts, 4, 5))
>T : Symbol(T, Decl(working.ts, 2, 23), Decl(working.ts, 5, 23))
>args : Symbol(args, Decl(working.ts, 2, 34))

throttle(): Function<T>;
>throttle : Symbol(Function.throttle, Decl(working.ts, 2, 57))
>Function : Symbol(Function, Decl(working.ts, 1, 21), Decl(working.ts, 4, 5))
>T : Symbol(T, Decl(working.ts, 2, 23), Decl(working.ts, 5, 23))
}
interface Function<T> {
>Function : Symbol(Function, Decl(working.ts, 1, 21), Decl(working.ts, 4, 5))
>T : Symbol(T, Decl(working.ts, 2, 23), Decl(working.ts, 5, 23))

unary(): Function<() => ReturnType<T>>;
>unary : Symbol(Function.unary, Decl(working.ts, 5, 27))
>Function : Symbol(Function, Decl(working.ts, 1, 21), Decl(working.ts, 4, 5))
>ReturnType : Symbol(ReturnType, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(working.ts, 2, 23), Decl(working.ts, 5, 23))
}
}
=== tests/cases/compiler/regression.ts ===
export namespace ns {
>ns : Symbol(ns, Decl(regression.ts, 0, 0))

interface Function<T> {
>Function : Symbol(Function, Decl(regression.ts, 0, 21), Decl(regression.ts, 3, 5))
>T : Symbol(T, Decl(regression.ts, 1, 23), Decl(regression.ts, 4, 23))

unary(): Function<() => ReturnType<T>>;
>unary : Symbol(Function.unary, Decl(regression.ts, 1, 27))
>Function : Symbol(Function, Decl(regression.ts, 0, 21), Decl(regression.ts, 3, 5))
>ReturnType : Symbol(ReturnType, Decl(lib.es5.d.ts, --, --))
>T : Symbol(T, Decl(regression.ts, 1, 23), Decl(regression.ts, 4, 23))
}
interface Function<T extends (...args: any) => any> {
>Function : Symbol(Function, Decl(regression.ts, 0, 21), Decl(regression.ts, 3, 5))
>T : Symbol(T, Decl(regression.ts, 1, 23), Decl(regression.ts, 4, 23))
>args : Symbol(args, Decl(regression.ts, 4, 34))

throttle(): Function<T>;
>throttle : Symbol(Function.throttle, Decl(regression.ts, 4, 57))
>Function : Symbol(Function, Decl(regression.ts, 0, 21), Decl(regression.ts, 3, 5))
>T : Symbol(T, Decl(regression.ts, 1, 23), Decl(regression.ts, 4, 23))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/working.ts ===
// minmal samples from #33395
export namespace ns {
interface Function<T extends (...args: any) => any> {
>args : any

throttle(): Function<T>;
>throttle : () => Function<T>
}
interface Function<T> {
unary(): Function<() => ReturnType<T>>;
>unary : () => Function<() => ReturnType<T>>
}
}
=== tests/cases/compiler/regression.ts ===
export namespace ns {
interface Function<T> {
unary(): Function<() => ReturnType<T>>;
>unary : () => Function<() => ReturnType<T>>
}
interface Function<T extends (...args: any) => any> {
>args : any

throttle(): Function<T>;
>throttle : () => Function<T>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ 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 (8 errors) ====
==== tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDifferentConstraints.ts (6 errors) ====
interface A<T extends Date> {
~
!!! error TS2428: All declarations of 'A' must have identical type parameters.
Expand Down Expand Up @@ -74,14 +72,10 @@ tests/cases/conformance/interfaces/declarationMerging/twoGenericInterfacesWithDi
}

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.
interface C<T extends number> { // ok
y: T;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ interface C<T> {
x: T;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ interface C<T> {
>T : Symbol(T, Decl(twoGenericInterfacesWithDifferentConstraints.ts, 52, 12), Decl(twoGenericInterfacesWithDifferentConstraints.ts, 56, 12))
}

interface C<T extends number> { // error
interface C<T extends number> { // ok
>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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface C<T> {
>x : T
}

interface C<T extends number> { // error
interface C<T extends number> { // ok
y: T;
>y : T
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @filename: working.ts
// minmal samples from #33395
export namespace ns {
interface Function<T extends (...args: any) => any> {
throttle(): Function<T>;
}
interface Function<T> {
unary(): Function<() => ReturnType<T>>;
}
}
// @filename: regression.ts
export namespace ns {
interface Function<T> {
unary(): Function<() => ReturnType<T>>;
}
interface Function<T extends (...args: any) => any> {
throttle(): Function<T>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface C<T> {
x: T;
}

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

Expand Down

0 comments on commit 5e06bea

Please sign in to comment.