Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getConstraintDeclaration gets the first declaration with a constraint… #33426

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8972,8 +8972,7 @@ namespace ts {
}

function getConstraintDeclaration(type: TypeParameter) {
const decl = type.symbol && getDeclarationOfKind<TypeParameterDeclaration>(type.symbol, SyntaxKind.TypeParameter);
return decl && getEffectiveConstraintOfTypeParameter(decl);
return mapDefined(filter(type.symbol && type.symbol.declarations, isTypeParameterDeclaration), getEffectiveConstraintOfTypeParameter)[0];
weswigham marked this conversation as resolved.
Show resolved Hide resolved
}

function getInferredTypeParameterConstraint(typeParameter: TypeParameter) {
Expand Down Expand Up @@ -29233,11 +29232,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
weswigham marked this conversation as resolved.
Show resolved Hide resolved
~
!!! error TS2428: All declarations of 'C' must have identical type parameters.
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>;
}
}