Skip to content

Commit bc3d515

Browse files
committed
Add related info suggesting a fix
1 parent 16f9460 commit bc3d515

18 files changed

+29
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16918,6 +16918,9 @@ namespace ts {
1691816918
return result;
1691916919
}
1692016920
reportRelationError(headMessage, source, target);
16921+
if (source.flags & TypeFlags.TypeVariable && source.symbol?.declarations?.[0] && !getConstraintOfType(<TypeVariable>source) && isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
16922+
associateRelatedInfo(createDiagnosticForNode(source.symbol.declarations[0], Diagnostics.This_type_parameter_likely_needs_an_extends_object_constraint));
16923+
}
1692116924
}
1692216925
}
1692316926
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,6 +3059,10 @@
30593059
"category": "Error",
30603060
"code": 2796
30613061
},
3062+
"This type parameter likely needs an `extends object` constraint.": {
3063+
"category": "Error",
3064+
"code": 2797
3065+
},
30623066

30633067
"Import declaration '{0}' is using private name '{1}'.": {
30643068
"category": "Error",

tests/baselines/reference/bestCommonTypeOfConditionalExpressions.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfC
3131
~~~~~~~~~~~~~~~~~~~~
3232
!!! error TS2322: Type 'T | U' is not assignable to type 'Object'.
3333
!!! error TS2322: Type 'T' is not assignable to type 'Object'.
34+
!!! related TS2797 tests/cases/conformance/types/typeRelationships/bestCommonType/bestCommonTypeOfConditionalExpressions.ts:24:15: This type parameter likely needs an `extends object` constraint.
3435
}

tests/baselines/reference/complexRecursiveCollections.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ tests/cases/compiler/immutable.ts(391,22): error TS2430: Interface 'Set<T>' inco
298298
export function Record<T>(defaultValues: T, name?: string): Record.Class<T>;
299299
~
300300
!!! error TS2344: Type 'T' does not satisfy the constraint 'Object'.
301+
!!! related TS2797 tests/cases/compiler/immutable.ts:261:26: This type parameter likely needs an `extends object` constraint.
301302
export module Seq {
302303
function isSeq(maybeSeq: any): maybeSeq is Seq.Indexed<any> | Seq.Keyed<any, any>;
303304
function of<T>(...values: Array<T>): Seq.Indexed<T>;

tests/baselines/reference/constructorReturningAPrimitive.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tests/cases/compiler/constructorReturningAPrimitive.ts(15,9): error TS2409: Retu
2020
return x;
2121
~~~~~~~~~
2222
!!! error TS2322: Type 'T' is not assignable to type 'B<T>'.
23+
!!! related TS2797 tests/cases/compiler/constructorReturningAPrimitive.ts:12:9: This type parameter likely needs an `extends object` constraint.
2324
~~~~~~~~~
2425
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
2526
}

tests/baselines/reference/keyofAndIndexedAccess.errors.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(619,13): error TS23
356356
a = x;
357357
~
358358
!!! error TS2322: Type 'T' is not assignable to type '{}'.
359+
!!! related TS2797 tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts:314:14: This type parameter likely needs an `extends object` constraint.
359360
a = y;
360361
~
361362
!!! error TS2322: Type 'T[keyof T]' is not assignable to type '{}'.
@@ -373,6 +374,8 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts(619,13): error TS23
373374
~
374375
!!! error TS2322: Type 'T' is not assignable to type '{} | null | undefined'.
375376
!!! error TS2322: Type 'T' is not assignable to type '{}'.
377+
!!! related TS2797 tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts:321:14: This type parameter likely needs an `extends object` constraint.
378+
!!! related TS2797 tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts:321:14: This type parameter likely needs an `extends object` constraint.
376379
a = y;
377380
~
378381
!!! error TS2322: Type 'T[keyof T]' is not assignable to type '{} | null | undefined'.

tests/baselines/reference/logicalOrOperatorWithTypeParameters.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ tests/cases/conformance/expressions/binaryOperators/logicalOrOperator/logicalOrO
1313
~~
1414
!!! error TS2322: Type 'T | U' is not assignable to type '{}'.
1515
!!! error TS2322: Type 'T' is not assignable to type '{}'.
16+
!!! related TS2797 tests/cases/conformance/expressions/binaryOperators/logicalOrOperator/logicalOrOperatorWithTypeParameters.ts:1:14: This type parameter likely needs an `extends object` constraint.
1617
}
1718

1819
function fn2<T, U/* extends T*/, V/* extends T*/>(t: T, u: U, v: V) {
@@ -25,6 +26,7 @@ tests/cases/conformance/expressions/binaryOperators/logicalOrOperator/logicalOrO
2526
~~
2627
!!! error TS2322: Type 'U | V' is not assignable to type '{}'.
2728
!!! error TS2322: Type 'U' is not assignable to type '{}'.
29+
!!! related TS2797 tests/cases/conformance/expressions/binaryOperators/logicalOrOperator/logicalOrOperatorWithTypeParameters.ts:8:17: This type parameter likely needs an `extends object` constraint.
2830
//var r7: T = u || v;
2931
}
3032

tests/baselines/reference/mappedTypesAndObjects.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts(25,11): error TS24
3333
!!! error TS2430: Interface 'E1<T>' incorrectly extends interface 'Base'.
3434
!!! error TS2430: Types of property 'foo' are incompatible.
3535
!!! error TS2430: Type 'T' is not assignable to type '{ [key: string]: any; }'.
36+
!!! related TS2797 tests/cases/conformance/types/mapped/mappedTypesAndObjects.ts:25:14: This type parameter likely needs an `extends object` constraint.
3637
foo: T;
3738
}
3839

tests/baselines/reference/subclassThisTypeAssignable.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tests/cases/compiler/tile1.ts(11,40): error TS2344: Type 'State' does not satisf
88
oninit?(vnode: Vnode<Attrs, State>): number;
99
~~~~~
1010
!!! error TS2344: Type 'State' does not satisfy the constraint 'Lifecycle<Attrs, State>'.
11+
!!! related TS2797 tests/cases/compiler/tile1.ts:1:28: This type parameter likely needs an `extends object` constraint.
1112
[_: number]: any;
1213
}
1314

@@ -21,6 +22,7 @@ tests/cases/compiler/tile1.ts(11,40): error TS2344: Type 'State' does not satisf
2122
view(this: State, vnode: Vnode<Attrs, State>): number;
2223
~~~~~
2324
!!! error TS2344: Type 'State' does not satisfy the constraint 'Lifecycle<Attrs, State>'.
25+
!!! related TS2797 tests/cases/compiler/tile1.ts:10:28: This type parameter likely needs an `extends object` constraint.
2426
}
2527

2628
interface ClassComponent<A> extends Lifecycle<A, ClassComponent<A>> {

tests/baselines/reference/subtypingWithOptionalProperties.errors.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW
99
var b: { s?: number } = a;
1010
~
1111
!!! error TS2322: Type 'T' is not assignable to type '{ s?: number; }'.
12+
!!! related TS2797 tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithOptionalProperties.ts:4:12: This type parameter likely needs an `extends object` constraint.
1213
return b;
1314
}
1415

0 commit comments

Comments
 (0)