Skip to content

Commit 72034e5

Browse files
committed
Remove circular constraint suggestions
1 parent 00ef5ae commit 72034e5

8 files changed

+6
-12
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18793,8 +18793,12 @@ namespace ts {
1879318793
}
1879418794
reportRelationError(headMessage, source, target);
1879518795
if (source.flags & TypeFlags.TypeParameter && source.symbol?.declarations?.[0] && !getConstraintOfType(source as TypeVariable)) {
18796-
const targetConstraintString = typeToString(target, source.symbol.declarations[0]);
18797-
associateRelatedInfo(createDiagnosticForNode(source.symbol.declarations[0], Diagnostics.This_type_parameter_might_need_an_extends_0_constraint, targetConstraintString));
18796+
const syntheticparam = cloneTypeParameter(source as TypeParameter);
18797+
syntheticparam.constraint = instantiateType(target, makeUnaryTypeMapper(source, syntheticparam));
18798+
if (hasNonCircularBaseConstraint(syntheticparam)) {
18799+
const targetConstraintString = typeToString(target, source.symbol.declarations[0]);
18800+
associateRelatedInfo(createDiagnosticForNode(source.symbol.declarations[0], Diagnostics.This_type_parameter_might_need_an_extends_0_constraint, targetConstraintString));
18801+
}
1879818802
}
1879918803
}
1880018804

tests/baselines/reference/conditionalTypes1.errors.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
8585
!!! error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
8686
!!! error TS2322: Type 'T' is not assignable to type '{}'.
8787
!!! related TS2208 tests/cases/conformance/types/conditional/conditionalTypes1.ts:10:13: This type parameter might need an `extends {}` constraint.
88-
!!! related TS2208 tests/cases/conformance/types/conditional/conditionalTypes1.ts:10:13: This type parameter might need an `extends NonNullable<T>` constraint.
8988
}
9089

9190
function f2<T extends string | undefined>(x: T, y: NonNullable<T>) {

tests/baselines/reference/conditionalTypes2.errors.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
8484
!!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
8585
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
8686
!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
87-
!!! related TS2208 tests/cases/conformance/types/conditional/conditionalTypes2.ts:23:13: This type parameter might need an `extends A extends string ? keyof A : A` constraint.
8887
b = a; // Error
8988
~
9089
!!! error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'.

tests/baselines/reference/deepComparisons.errors.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ tests/cases/compiler/deepComparisons.ts(4,9): error TS2322: Type 'T[K1][K2]' is
1717
let v1: Extract<T, string> = 0 as any as T; // Error
1818
~~
1919
!!! error TS2322: Type 'T' is not assignable to type 'Extract<T, string>'.
20-
!!! related TS2208 tests/cases/compiler/deepComparisons.ts:1:13: This type parameter might need an `extends Extract<T, string>` constraint.
2120
let v2: Extract<T[K1], string> = 0 as any as T[K1]; // Error
2221
~~
2322
!!! error TS2322: Type 'T[K1]' is not assignable to type 'Extract<T[K1], string>'.

tests/baselines/reference/flatArrayNoExcessiveStackDepth.errors.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,5 @@ tests/cases/compiler/flatArrayNoExcessiveStackDepth.ts(20,5): error TS2322: Type
4040
!!! error TS2322: Type 'InnerArr' is not assignable to type 'FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][D]>'.
4141
!!! error TS2322: Type 'InnerArr' is not assignable to type '(InnerArr extends readonly (infer InnerArr)[] ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][D]]> : InnerArr) & InnerArr'.
4242
!!! error TS2322: Type 'InnerArr' is not assignable to type 'InnerArr extends readonly (infer InnerArr)[] ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][D]]> : InnerArr'.
43-
!!! related TS2208 /.ts/lib.es2019.array.d.ts:23:46: This type parameter might need an `extends InnerArr extends readonly (infer InnerArr)[] ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][D]]> : InnerArr` constraint.
44-
!!! related TS2208 /.ts/lib.es2019.array.d.ts:23:46: This type parameter might need an `extends (InnerArr extends readonly (infer InnerArr)[] ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][D]]> : InnerArr) & InnerArr` constraint.
45-
!!! related TS2208 /.ts/lib.es2019.array.d.ts:23:46: This type parameter might need an `extends FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][D]>` constraint.
4643
}
4744

tests/baselines/reference/intersectionTypeInference.errors.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ tests/cases/conformance/types/intersection/intersectionTypeInference.ts(6,5): er
1717
!!! error TS2322: Type 'T' is not assignable to type 'U'.
1818
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
1919
!!! related TS2208 tests/cases/conformance/types/intersection/intersectionTypeInference.ts:1:17: This type parameter might need an `extends U` constraint.
20-
!!! related TS2208 tests/cases/conformance/types/intersection/intersectionTypeInference.ts:1:17: This type parameter might need an `extends T & U` constraint.
2120
result = obj2; // Error
2221
~~~~~~
2322
!!! error TS2322: Type 'U' is not assignable to type 'T & U'.
2423
!!! error TS2322: Type 'U' is not assignable to type 'T'.
2524
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
2625
!!! related TS2208 tests/cases/conformance/types/intersection/intersectionTypeInference.ts:1:20: This type parameter might need an `extends T` constraint.
27-
!!! related TS2208 tests/cases/conformance/types/intersection/intersectionTypeInference.ts:1:20: This type parameter might need an `extends T & U` constraint.
2826
return result;
2927
}
3028

tests/baselines/reference/recursiveConditionalTypes.errors.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ tests/cases/compiler/recursiveConditionalTypes.ts(169,5): error TS2322: Type 'nu
5757
ta = tx; // Error
5858
~~
5959
!!! error TS2322: Type 'T' is not assignable to type '__Awaited<T>'.
60-
!!! related TS2208 tests/cases/compiler/recursiveConditionalTypes.ts:18:14: This type parameter might need an `extends __Awaited<T>` constraint.
6160
tx = ta; // Error
6261
~~
6362
!!! error TS2322: Type '__Awaited<T>' is not assignable to type 'T'.

tests/baselines/reference/tsxNotUsingApparentTypeOfSFC.errors.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx(18,14): error TS2769: No o
4646
!!! error TS2322: Type 'P' is not assignable to type 'IntrinsicAttributes & P'.
4747
!!! error TS2322: Type 'P' is not assignable to type 'IntrinsicAttributes'.
4848
!!! related TS2208 tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx:5:15: This type parameter might need an `extends JSX.IntrinsicAttributes` constraint.
49-
!!! related TS2208 tests/cases/compiler/tsxNotUsingApparentTypeOfSFC.tsx:5:15: This type parameter might need an `extends JSX.IntrinsicAttributes & P` constraint.
5049
let q = <MyComponent {...wrappedProps} /> // should work
5150
~~~~~~~~~~~
5251
!!! error TS2769: No overload matches this call.

0 commit comments

Comments
 (0)