Skip to content

Commit 40ea667

Browse files
authored
Merge pull request microsoft#10961 from YuichiNukiyama/change_errormessage
change error message for assigning from object
2 parents 070aa83 + c1aff4f commit 40ea667

13 files changed

+143
-36
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6502,6 +6502,9 @@ namespace ts {
65026502
if (source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.Primitive) {
65036503
tryElaborateErrorsForPrimitivesAndObjects(source, target);
65046504
}
6505+
else if (source.symbol && source.flags & TypeFlags.ObjectType && globalObjectType === source) {
6506+
reportError(Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead);
6507+
}
65056508
reportRelationError(headMessage, source, target);
65066509
}
65076510
return Ternary.False;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,10 @@
19591959
"category": "Error",
19601960
"code": 2695
19611961
},
1962+
"The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?": {
1963+
"category": "Error",
1964+
"code": 2696
1965+
},
19621966

19631967
"Import declaration '{0}' is using private name '{1}'.": {
19641968
"category": "Error",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(3,1): error TS2322: Type 'Object' is not assignable to type 'RegExp'.
2+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
3+
Property 'exec' is missing in type 'Object'.
4+
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(5,5): error TS2322: Type 'Object' is not assignable to type 'String'.
5+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
6+
Property 'charAt' is missing in type 'Object'.
7+
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(6,5): error TS2322: Type 'Number' is not assignable to type 'String'.
8+
Property 'charAt' is missing in type 'Number'.
9+
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(8,5): error TS2322: Type 'Object' is not assignable to type 'Error'.
10+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
11+
Property 'name' is missing in type 'Object'.
12+
13+
14+
==== tests/cases/compiler/assigningFromObjectToAnythingElse.ts (4 errors) ====
15+
var x: Object;
16+
var y: RegExp;
17+
y = x;
18+
~
19+
!!! error TS2322: Type 'Object' is not assignable to type 'RegExp'.
20+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
21+
!!! error TS2322: Property 'exec' is missing in type 'Object'.
22+
23+
var a: String = Object.create<Object>("");
24+
~
25+
!!! error TS2322: Type 'Object' is not assignable to type 'String'.
26+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
27+
!!! error TS2322: Property 'charAt' is missing in type 'Object'.
28+
var c: String = Object.create<Number>(1);
29+
~
30+
!!! error TS2322: Type 'Number' is not assignable to type 'String'.
31+
!!! error TS2322: Property 'charAt' is missing in type 'Number'.
32+
33+
var w: Error = new Object();
34+
~
35+
!!! error TS2322: Type 'Object' is not assignable to type 'Error'.
36+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
37+
!!! error TS2322: Property 'name' is missing in type 'Object'.
38+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [assigningFromObjectToAnythingElse.ts]
2+
var x: Object;
3+
var y: RegExp;
4+
y = x;
5+
6+
var a: String = Object.create<Object>("");
7+
var c: String = Object.create<Number>(1);
8+
9+
var w: Error = new Object();
10+
11+
12+
//// [assigningFromObjectToAnythingElse.js]
13+
var x;
14+
var y;
15+
y = x;
16+
var a = Object.create("");
17+
var c = Object.create(1);
18+
var w = new Object();

tests/baselines/reference/intTypeCheck.errors.txt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ tests/cases/compiler/intTypeCheck.ts(35,6): error TS2304: Cannot find name 'p'.
22
tests/cases/compiler/intTypeCheck.ts(71,6): error TS2304: Cannot find name 'p'.
33
tests/cases/compiler/intTypeCheck.ts(85,5): error TS2386: Overload signatures must all be optional or required.
44
tests/cases/compiler/intTypeCheck.ts(99,5): error TS2322: Type 'Object' is not assignable to type 'i1'.
5-
Property 'p' is missing in type 'Object'.
5+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
6+
Property 'p' is missing in type 'Object'.
67
tests/cases/compiler/intTypeCheck.ts(100,16): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
78
tests/cases/compiler/intTypeCheck.ts(101,5): error TS2322: Type 'Base' is not assignable to type 'i1'.
89
Property 'p' is missing in type 'Base'.
@@ -15,7 +16,8 @@ tests/cases/compiler/intTypeCheck.ts(107,17): error TS2351: Cannot use 'new' wit
1516
tests/cases/compiler/intTypeCheck.ts(112,5): error TS2322: Type '{}' is not assignable to type 'i2'.
1617
Type '{}' provides no match for the signature '(): any'
1718
tests/cases/compiler/intTypeCheck.ts(113,5): error TS2322: Type 'Object' is not assignable to type 'i2'.
18-
Type 'Object' provides no match for the signature '(): any'
19+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
20+
Type 'Object' provides no match for the signature '(): any'
1921
tests/cases/compiler/intTypeCheck.ts(114,17): error TS2350: Only a void function can be called with the 'new' keyword.
2022
tests/cases/compiler/intTypeCheck.ts(115,5): error TS2322: Type 'Base' is not assignable to type 'i2'.
2123
Type 'Base' provides no match for the signature '(): any'
@@ -26,7 +28,8 @@ tests/cases/compiler/intTypeCheck.ts(121,17): error TS2351: Cannot use 'new' wit
2628
tests/cases/compiler/intTypeCheck.ts(126,5): error TS2322: Type '{}' is not assignable to type 'i3'.
2729
Type '{}' provides no match for the signature 'new (): any'
2830
tests/cases/compiler/intTypeCheck.ts(127,5): error TS2322: Type 'Object' is not assignable to type 'i3'.
29-
Type 'Object' provides no match for the signature 'new (): any'
31+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
32+
Type 'Object' provides no match for the signature 'new (): any'
3033
tests/cases/compiler/intTypeCheck.ts(129,5): error TS2322: Type 'Base' is not assignable to type 'i3'.
3134
Type 'Base' provides no match for the signature 'new (): any'
3235
tests/cases/compiler/intTypeCheck.ts(131,5): error TS2322: Type '() => void' is not assignable to type 'i3'.
@@ -43,7 +46,8 @@ tests/cases/compiler/intTypeCheck.ts(149,17): error TS2351: Cannot use 'new' wit
4346
tests/cases/compiler/intTypeCheck.ts(154,5): error TS2322: Type '{}' is not assignable to type 'i5'.
4447
Property 'p' is missing in type '{}'.
4548
tests/cases/compiler/intTypeCheck.ts(155,5): error TS2322: Type 'Object' is not assignable to type 'i5'.
46-
Property 'p' is missing in type 'Object'.
49+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
50+
Property 'p' is missing in type 'Object'.
4751
tests/cases/compiler/intTypeCheck.ts(156,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
4852
tests/cases/compiler/intTypeCheck.ts(157,5): error TS2322: Type 'Base' is not assignable to type 'i5'.
4953
Property 'p' is missing in type 'Base'.
@@ -56,7 +60,8 @@ tests/cases/compiler/intTypeCheck.ts(163,17): error TS2351: Cannot use 'new' wit
5660
tests/cases/compiler/intTypeCheck.ts(168,5): error TS2322: Type '{}' is not assignable to type 'i6'.
5761
Type '{}' provides no match for the signature '(): any'
5862
tests/cases/compiler/intTypeCheck.ts(169,5): error TS2322: Type 'Object' is not assignable to type 'i6'.
59-
Type 'Object' provides no match for the signature '(): any'
63+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
64+
Type 'Object' provides no match for the signature '(): any'
6065
tests/cases/compiler/intTypeCheck.ts(170,17): error TS2350: Only a void function can be called with the 'new' keyword.
6166
tests/cases/compiler/intTypeCheck.ts(171,5): error TS2322: Type 'Base' is not assignable to type 'i6'.
6267
Type 'Base' provides no match for the signature '(): any'
@@ -69,7 +74,8 @@ tests/cases/compiler/intTypeCheck.ts(177,17): error TS2351: Cannot use 'new' wit
6974
tests/cases/compiler/intTypeCheck.ts(182,5): error TS2322: Type '{}' is not assignable to type 'i7'.
7075
Type '{}' provides no match for the signature 'new (): any'
7176
tests/cases/compiler/intTypeCheck.ts(183,5): error TS2322: Type 'Object' is not assignable to type 'i7'.
72-
Type 'Object' provides no match for the signature 'new (): any'
77+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
78+
Type 'Object' provides no match for the signature 'new (): any'
7379
tests/cases/compiler/intTypeCheck.ts(185,17): error TS2352: Type 'Base' cannot be converted to type 'i7'.
7480
Type 'Base' provides no match for the signature 'new (): any'
7581
tests/cases/compiler/intTypeCheck.ts(187,5): error TS2322: Type '() => void' is not assignable to type 'i7'.
@@ -193,7 +199,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
193199
var obj2: i1 = new Object();
194200
~~~~
195201
!!! error TS2322: Type 'Object' is not assignable to type 'i1'.
196-
!!! error TS2322: Property 'p' is missing in type 'Object'.
202+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
203+
!!! error TS2322: Property 'p' is missing in type 'Object'.
197204
var obj3: i1 = new obj0;
198205
~~~~~~~~
199206
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
@@ -229,7 +236,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
229236
var obj13: i2 = new Object();
230237
~~~~~
231238
!!! error TS2322: Type 'Object' is not assignable to type 'i2'.
232-
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
239+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
240+
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
233241
var obj14: i2 = new obj11;
234242
~~~~~~~~~
235243
!!! error TS2350: Only a void function can be called with the 'new' keyword.
@@ -262,7 +270,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
262270
var obj24: i3 = new Object();
263271
~~~~~
264272
!!! error TS2322: Type 'Object' is not assignable to type 'i3'.
265-
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
273+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
274+
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
266275
var obj25: i3 = new obj22;
267276
var obj26: i3 = new Base;
268277
~~~~~
@@ -320,7 +329,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
320329
var obj46: i5 = new Object();
321330
~~~~~
322331
!!! error TS2322: Type 'Object' is not assignable to type 'i5'.
323-
!!! error TS2322: Property 'p' is missing in type 'Object'.
332+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
333+
!!! error TS2322: Property 'p' is missing in type 'Object'.
324334
var obj47: i5 = new obj44;
325335
~~~~~~~~~
326336
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
@@ -356,7 +366,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
356366
var obj57: i6 = new Object();
357367
~~~~~
358368
!!! error TS2322: Type 'Object' is not assignable to type 'i6'.
359-
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
369+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
370+
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
360371
var obj58: i6 = new obj55;
361372
~~~~~~~~~
362373
!!! error TS2350: Only a void function can be called with the 'new' keyword.
@@ -392,7 +403,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
392403
var obj68: i7 = new Object();
393404
~~~~~
394405
!!! error TS2322: Type 'Object' is not assignable to type 'i7'.
395-
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
406+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
407+
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
396408
var obj69: i7 = new obj66;
397409
var obj70: i7 = <i7>new Base;
398410
~~~~~~~~~~~~

tests/baselines/reference/objectTypeHidingMembersOfObjectAssignmentCompat2.errors.txt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC
33
Type '() => number' is not assignable to type '() => string'.
44
Type 'number' is not assignable to type 'string'.
55
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(8,1): error TS2322: Type 'Object' is not assignable to type 'I'.
6-
Types of property 'toString' are incompatible.
7-
Type '() => string' is not assignable to type '() => number'.
8-
Type 'string' is not assignable to type 'number'.
6+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
7+
Types of property 'toString' are incompatible.
8+
Type '() => string' is not assignable to type '() => number'.
9+
Type 'string' is not assignable to type 'number'.
910
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(14,1): error TS2322: Type 'C' is not assignable to type 'Object'.
1011
Types of property 'toString' are incompatible.
1112
Type '() => number' is not assignable to type '() => string'.
1213
Type 'number' is not assignable to type 'string'.
1314
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(15,1): error TS2322: Type 'Object' is not assignable to type 'C'.
14-
Types of property 'toString' are incompatible.
15-
Type '() => string' is not assignable to type '() => number'.
16-
Type 'string' is not assignable to type 'number'.
15+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
16+
Types of property 'toString' are incompatible.
17+
Type '() => string' is not assignable to type '() => number'.
18+
Type 'string' is not assignable to type 'number'.
1719
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(20,1): error TS2322: Type '{ toString: () => void; }' is not assignable to type 'Object'.
1820
Types of property 'toString' are incompatible.
1921
Type '() => void' is not assignable to type '() => string'.
@@ -36,9 +38,10 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC
3638
i = o; // error
3739
~
3840
!!! error TS2322: Type 'Object' is not assignable to type 'I'.
39-
!!! error TS2322: Types of property 'toString' are incompatible.
40-
!!! error TS2322: Type '() => string' is not assignable to type '() => number'.
41-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
41+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
42+
!!! error TS2322: Types of property 'toString' are incompatible.
43+
!!! error TS2322: Type '() => string' is not assignable to type '() => number'.
44+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
4245

4346
class C {
4447
toString(): number { return 1; }
@@ -53,9 +56,10 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC
5356
c = o; // error
5457
~
5558
!!! error TS2322: Type 'Object' is not assignable to type 'C'.
56-
!!! error TS2322: Types of property 'toString' are incompatible.
57-
!!! error TS2322: Type '() => string' is not assignable to type '() => number'.
58-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
59+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
60+
!!! error TS2322: Types of property 'toString' are incompatible.
61+
!!! error TS2322: Type '() => string' is not assignable to type '() => number'.
62+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
5963

6064
var a = {
6165
toString: () => { }

tests/baselines/reference/objectTypeWithCallSignatureHidingMembersOfFunctionAssignmentCompat.errors.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOfFunctionAssignmentCompat.ts(8,1): error TS2322: Type 'Object' is not assignable to type 'I'.
2-
Type 'Object' provides no match for the signature '(): void'
2+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
3+
Type 'Object' provides no match for the signature '(): void'
34
tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOfFunctionAssignmentCompat.ts(14,1): error TS2322: Type 'Object' is not assignable to type '() => void'.
4-
Type 'Object' provides no match for the signature '(): void'
5+
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
6+
Type 'Object' provides no match for the signature '(): void'
57

68

79
==== tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOfFunctionAssignmentCompat.ts (2 errors) ====
@@ -15,7 +17,8 @@ tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOf
1517
i = f;
1618
~
1719
!!! error TS2322: Type 'Object' is not assignable to type 'I'.
18-
!!! error TS2322: Type 'Object' provides no match for the signature '(): void'
20+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
21+
!!! error TS2322: Type 'Object' provides no match for the signature '(): void'
1922

2023
var a: {
2124
(): void
@@ -24,4 +27,5 @@ tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOf
2427
a = f;
2528
~
2629
!!! error TS2322: Type 'Object' is not assignable to type '() => void'.
27-
!!! error TS2322: Type 'Object' provides no match for the signature '(): void'
30+
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
31+
!!! error TS2322: Type 'Object' provides no match for the signature '(): void'

0 commit comments

Comments
 (0)