Skip to content

change error message for assigning from object #10961

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

Merged
merged 4 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6502,6 +6502,9 @@ namespace ts {
if (source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.Primitive) {
tryElaborateErrorsForPrimitivesAndObjects(source, target);
}
else if (source.symbol && source.flags & TypeFlags.ObjectType && globalObjectType === source) {
reportError(Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead);
}
reportRelationError(headMessage, source, target);
}
return Ternary.False;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,10 @@
"category": "Error",
"code": 2695
},
"The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?": {
"category": "Error",
"code": 2696
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(3,1): error TS2322: Type 'Object' is not assignable to type 'RegExp'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'exec' is missing in type 'Object'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(5,5): error TS2322: Type 'Object' is not assignable to type 'String'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'charAt' is missing in type 'Object'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(6,5): error TS2322: Type 'Number' is not assignable to type 'String'.
Property 'charAt' is missing in type 'Number'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(8,5): error TS2322: Type 'Object' is not assignable to type 'Error'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'name' is missing in type 'Object'.


==== tests/cases/compiler/assigningFromObjectToAnythingElse.ts (4 errors) ====
var x: Object;
var y: RegExp;
y = x;
~
!!! error TS2322: Type 'Object' is not assignable to type 'RegExp'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Property 'exec' is missing in type 'Object'.

var a: String = Object.create<Object>("");
~
!!! error TS2322: Type 'Object' is not assignable to type 'String'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Property 'charAt' is missing in type 'Object'.
var c: String = Object.create<Number>(1);
~
!!! error TS2322: Type 'Number' is not assignable to type 'String'.
!!! error TS2322: Property 'charAt' is missing in type 'Number'.

var w: Error = new Object();
~
!!! error TS2322: Type 'Object' is not assignable to type 'Error'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Property 'name' is missing in type 'Object'.

18 changes: 18 additions & 0 deletions tests/baselines/reference/assigningFromObjectToAnythingElse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [assigningFromObjectToAnythingElse.ts]
var x: Object;
var y: RegExp;
y = x;

var a: String = Object.create<Object>("");
var c: String = Object.create<Number>(1);

var w: Error = new Object();


//// [assigningFromObjectToAnythingElse.js]
var x;
var y;
y = x;
var a = Object.create("");
var c = Object.create(1);
var w = new Object();
36 changes: 24 additions & 12 deletions tests/baselines/reference/intTypeCheck.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ tests/cases/compiler/intTypeCheck.ts(35,6): error TS2304: Cannot find name 'p'.
tests/cases/compiler/intTypeCheck.ts(71,6): error TS2304: Cannot find name 'p'.
tests/cases/compiler/intTypeCheck.ts(85,5): error TS2386: Overload signatures must all be optional or required.
tests/cases/compiler/intTypeCheck.ts(99,5): error TS2322: Type 'Object' is not assignable to type 'i1'.
Property 'p' is missing in type 'Object'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'p' is missing in type 'Object'.
tests/cases/compiler/intTypeCheck.ts(100,16): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
tests/cases/compiler/intTypeCheck.ts(101,5): error TS2322: Type 'Base' is not assignable to type 'i1'.
Property 'p' is missing in type 'Base'.
Expand All @@ -15,7 +16,8 @@ tests/cases/compiler/intTypeCheck.ts(107,17): error TS2351: Cannot use 'new' wit
tests/cases/compiler/intTypeCheck.ts(112,5): error TS2322: Type '{}' is not assignable to type 'i2'.
Type '{}' provides no match for the signature '(): any'
tests/cases/compiler/intTypeCheck.ts(113,5): error TS2322: Type 'Object' is not assignable to type 'i2'.
Type 'Object' provides no match for the signature '(): any'
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' provides no match for the signature '(): any'
tests/cases/compiler/intTypeCheck.ts(114,17): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/compiler/intTypeCheck.ts(115,5): error TS2322: Type 'Base' is not assignable to type 'i2'.
Type 'Base' provides no match for the signature '(): any'
Expand All @@ -26,7 +28,8 @@ tests/cases/compiler/intTypeCheck.ts(121,17): error TS2351: Cannot use 'new' wit
tests/cases/compiler/intTypeCheck.ts(126,5): error TS2322: Type '{}' is not assignable to type 'i3'.
Type '{}' provides no match for the signature 'new (): any'
tests/cases/compiler/intTypeCheck.ts(127,5): error TS2322: Type 'Object' is not assignable to type 'i3'.
Type 'Object' provides no match for the signature 'new (): any'
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' provides no match for the signature 'new (): any'
tests/cases/compiler/intTypeCheck.ts(129,5): error TS2322: Type 'Base' is not assignable to type 'i3'.
Type 'Base' provides no match for the signature 'new (): any'
tests/cases/compiler/intTypeCheck.ts(131,5): error TS2322: Type '() => void' is not assignable to type 'i3'.
Expand All @@ -43,7 +46,8 @@ tests/cases/compiler/intTypeCheck.ts(149,17): error TS2351: Cannot use 'new' wit
tests/cases/compiler/intTypeCheck.ts(154,5): error TS2322: Type '{}' is not assignable to type 'i5'.
Property 'p' is missing in type '{}'.
tests/cases/compiler/intTypeCheck.ts(155,5): error TS2322: Type 'Object' is not assignable to type 'i5'.
Property 'p' is missing in type 'Object'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'p' is missing in type 'Object'.
tests/cases/compiler/intTypeCheck.ts(156,17): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
tests/cases/compiler/intTypeCheck.ts(157,5): error TS2322: Type 'Base' is not assignable to type 'i5'.
Property 'p' is missing in type 'Base'.
Expand All @@ -56,7 +60,8 @@ tests/cases/compiler/intTypeCheck.ts(163,17): error TS2351: Cannot use 'new' wit
tests/cases/compiler/intTypeCheck.ts(168,5): error TS2322: Type '{}' is not assignable to type 'i6'.
Type '{}' provides no match for the signature '(): any'
tests/cases/compiler/intTypeCheck.ts(169,5): error TS2322: Type 'Object' is not assignable to type 'i6'.
Type 'Object' provides no match for the signature '(): any'
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' provides no match for the signature '(): any'
tests/cases/compiler/intTypeCheck.ts(170,17): error TS2350: Only a void function can be called with the 'new' keyword.
tests/cases/compiler/intTypeCheck.ts(171,5): error TS2322: Type 'Base' is not assignable to type 'i6'.
Type 'Base' provides no match for the signature '(): any'
Expand All @@ -69,7 +74,8 @@ tests/cases/compiler/intTypeCheck.ts(177,17): error TS2351: Cannot use 'new' wit
tests/cases/compiler/intTypeCheck.ts(182,5): error TS2322: Type '{}' is not assignable to type 'i7'.
Type '{}' provides no match for the signature 'new (): any'
tests/cases/compiler/intTypeCheck.ts(183,5): error TS2322: Type 'Object' is not assignable to type 'i7'.
Type 'Object' provides no match for the signature 'new (): any'
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' provides no match for the signature 'new (): any'
tests/cases/compiler/intTypeCheck.ts(185,17): error TS2352: Type 'Base' cannot be converted to type 'i7'.
Type 'Base' provides no match for the signature 'new (): any'
tests/cases/compiler/intTypeCheck.ts(187,5): error TS2322: Type '() => void' is not assignable to type 'i7'.
Expand Down Expand Up @@ -193,7 +199,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj2: i1 = new Object();
~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i1'.
!!! error TS2322: Property 'p' is missing in type 'Object'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Property 'p' is missing in type 'Object'.
var obj3: i1 = new obj0;
~~~~~~~~
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
Expand Down Expand Up @@ -229,7 +236,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj13: i2 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i2'.
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
var obj14: i2 = new obj11;
~~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
Expand Down Expand Up @@ -262,7 +270,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj24: i3 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i3'.
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
var obj25: i3 = new obj22;
var obj26: i3 = new Base;
~~~~~
Expand Down Expand Up @@ -320,7 +329,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj46: i5 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i5'.
!!! error TS2322: Property 'p' is missing in type 'Object'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Property 'p' is missing in type 'Object'.
var obj47: i5 = new obj44;
~~~~~~~~~
!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.
Expand Down Expand Up @@ -356,7 +366,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj57: i6 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i6'.
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Type 'Object' provides no match for the signature '(): any'
var obj58: i6 = new obj55;
~~~~~~~~~
!!! error TS2350: Only a void function can be called with the 'new' keyword.
Expand Down Expand Up @@ -392,7 +403,8 @@ tests/cases/compiler/intTypeCheck.ts(205,17): error TS2351: Cannot use 'new' wit
var obj68: i7 = new Object();
~~~~~
!!! error TS2322: Type 'Object' is not assignable to type 'i7'.
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Type 'Object' provides no match for the signature 'new (): any'
var obj69: i7 = new obj66;
var obj70: i7 = <i7>new Base;
~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC
Type '() => number' is not assignable to type '() => string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(8,1): error TS2322: Type 'Object' is not assignable to type 'I'.
Types of property 'toString' are incompatible.
Type '() => string' is not assignable to type '() => number'.
Type 'string' is not assignable to type 'number'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Types of property 'toString' are incompatible.
Type '() => string' is not assignable to type '() => number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(14,1): error TS2322: Type 'C' is not assignable to type 'Object'.
Types of property 'toString' are incompatible.
Type '() => number' is not assignable to type '() => string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(15,1): error TS2322: Type 'Object' is not assignable to type 'C'.
Types of property 'toString' are incompatible.
Type '() => string' is not assignable to type '() => number'.
Type 'string' is not assignable to type 'number'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Types of property 'toString' are incompatible.
Type '() => string' is not assignable to type '() => number'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentCompat2.ts(20,1): error TS2322: Type '{ toString: () => void; }' is not assignable to type 'Object'.
Types of property 'toString' are incompatible.
Type '() => void' is not assignable to type '() => string'.
Expand All @@ -36,9 +38,10 @@ tests/cases/conformance/types/members/objectTypeHidingMembersOfObjectAssignmentC
i = o; // error
~
!!! error TS2322: Type 'Object' is not assignable to type 'I'.
!!! error TS2322: Types of property 'toString' are incompatible.
!!! error TS2322: Type '() => string' is not assignable to type '() => number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Types of property 'toString' are incompatible.
!!! error TS2322: Type '() => string' is not assignable to type '() => number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.

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

var a = {
toString: () => { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOfFunctionAssignmentCompat.ts(8,1): error TS2322: Type 'Object' is not assignable to type 'I'.
Type 'Object' provides no match for the signature '(): void'
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' provides no match for the signature '(): void'
tests/cases/conformance/types/members/objectTypeWithCallSignatureHidingMembersOfFunctionAssignmentCompat.ts(14,1): error TS2322: Type 'Object' is not assignable to type '() => void'.
Type 'Object' provides no match for the signature '(): void'
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' provides no match for the signature '(): void'


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

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