Skip to content

Commit 90b345c

Browse files
Merge pull request #4575 from Microsoft/bettErrErrorsForObjectLiteralExcessPropertiesInMaster
Better error spans for object literal excess properties (into branch 'master')
2 parents d0281f0 + ad3cca3 commit 90b345c

32 files changed

+107
-105
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4548,7 +4548,7 @@ namespace ts {
45484548
* @param target The right-hand-side of the relation.
45494549
* @param relation The relation considered. One of 'identityRelation', 'assignableRelation', or 'subTypeRelation'.
45504550
* Used as both to determine which checks are performed and as a cache of previously computed results.
4551-
* @param errorNode The node upon which all errors will be reported, if defined.
4551+
* @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used.
45524552
* @param headMessage If the error chain should be prepended by a head message, then headMessage will be used.
45534553
* @param containingMessageChain A chain of errors to prepend any new errors found.
45544554
*/
@@ -4767,7 +4767,13 @@ namespace ts {
47674767
for (let prop of getPropertiesOfObjectType(source)) {
47684768
if (!isKnownProperty(target, prop.name)) {
47694769
if (reportErrors) {
4770-
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(prop), typeToString(target));
4770+
// We know *exactly* where things went wrong when comparing the types.
4771+
// Use this property as the error node as this will be more helpful in
4772+
// reasoning about what went wrong.
4773+
errorNode = prop.valueDeclaration
4774+
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
4775+
symbolToString(prop),
4776+
typeToString(target));
47714777
}
47724778
return true;
47734779
}

tests/baselines/reference/arrayCast.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/arrayCast.ts(3,1): error TS2352: Neither type '{ foo: string; }[]' nor type '{ id: number; }[]' is assignable to the other.
1+
tests/cases/compiler/arrayCast.ts(3,23): error TS2352: Neither type '{ foo: string; }[]' nor type '{ id: number; }[]' is assignable to the other.
22
Type '{ foo: string; }' is not assignable to type '{ id: number; }'.
33
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
44

@@ -7,7 +7,7 @@ tests/cases/compiler/arrayCast.ts(3,1): error TS2352: Neither type '{ foo: strin
77
// Should fail. Even though the array is contextually typed with { id: number }[], it still
88
// has type { foo: string }[], which is not assignable to { id: number }[].
99
<{ id: number; }[]>[{ foo: "s" }];
10-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
~~~~~~~~
1111
!!! error TS2352: Neither type '{ foo: string; }[]' nor type '{ id: number; }[]' is assignable to the other.
1212
!!! error TS2352: Type '{ foo: string; }' is not assignable to type '{ id: number; }'.
1313
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.

tests/baselines/reference/arrayLiteralTypeInference.errors.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/compiler/arrayLiteralTypeInference.ts(13,5): error TS2322: Type '({ id: number; trueness: boolean; } | { id: number; name: string; })[]' is not assignable to type 'Action[]'.
1+
tests/cases/compiler/arrayLiteralTypeInference.ts(14,14): error TS2322: Type '({ id: number; trueness: boolean; } | { id: number; name: string; })[]' is not assignable to type 'Action[]'.
22
Type '{ id: number; trueness: boolean; } | { id: number; name: string; }' is not assignable to type 'Action'.
33
Type '{ id: number; trueness: boolean; }' is not assignable to type 'Action'.
44
Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
5-
tests/cases/compiler/arrayLiteralTypeInference.ts(29,5): error TS2322: Type '({ id: number; trueness: boolean; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
5+
tests/cases/compiler/arrayLiteralTypeInference.ts(31,18): error TS2322: Type '({ id: number; trueness: boolean; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
66
Type '{ id: number; trueness: boolean; } | { id: number; name: string; }' is not assignable to type '{ id: number; }'.
77
Type '{ id: number; trueness: boolean; }' is not assignable to type '{ id: number; }'.
88
Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
@@ -22,12 +22,12 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(29,5): error TS2322: Type '({
2222
}
2323

2424
var x1: Action[] = [
25-
~~
25+
{ id: 2, trueness: false },
26+
~~~~~~~~~~~~~~~
2627
!!! error TS2322: Type '({ id: number; trueness: boolean; } | { id: number; name: string; })[]' is not assignable to type 'Action[]'.
2728
!!! error TS2322: Type '{ id: number; trueness: boolean; } | { id: number; name: string; }' is not assignable to type 'Action'.
2829
!!! error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type 'Action'.
2930
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
30-
{ id: 2, trueness: false },
3131
{ id: 3, name: "three" }
3232
]
3333

@@ -43,13 +43,13 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(29,5): error TS2322: Type '({
4343
]
4444

4545
var z1: { id: number }[] =
46-
~~
46+
[
47+
{ id: 2, trueness: false },
48+
~~~~~~~~~~~~~~~
4749
!!! error TS2322: Type '({ id: number; trueness: boolean; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
4850
!!! error TS2322: Type '{ id: number; trueness: boolean; } | { id: number; name: string; }' is not assignable to type '{ id: number; }'.
4951
!!! error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type '{ id: number; }'.
5052
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
51-
[
52-
{ id: 2, trueness: false },
5353
{ id: 3, name: "three" }
5454
]
5555

tests/baselines/reference/arrayLiterals.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,5): error TS2322: Type '({ a: string; b: number; c: string; } | { a: string; b: number; c: number; })[]' is not assignable to type '{ [n: number]: { a: string; b: number; }; }'.
1+
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,77): error TS2322: Type '({ a: string; b: number; c: string; } | { a: string; b: number; c: number; })[]' is not assignable to type '{ [n: number]: { a: string; b: number; }; }'.
22
Index signatures are incompatible.
33
Type '{ a: string; b: number; c: string; } | { a: string; b: number; c: number; }' is not assignable to type '{ a: string; b: number; }'.
44
Type '{ a: string; b: number; c: string; }' is not assignable to type '{ a: string; b: number; }'.
@@ -30,7 +30,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,5): error
3030

3131
// Contextual type C with numeric index signature makes array literal of EveryType E of type BCT(E,C)[]
3232
var context1: { [n: number]: { a: string; b: number; }; } = [{ a: '', b: 0, c: '' }, { a: "", b: 3, c: 0 }];
33-
~~~~~~~~
33+
~~~~~
3434
!!! error TS2322: Type '({ a: string; b: number; c: string; } | { a: string; b: number; c: number; })[]' is not assignable to type '{ [n: number]: { a: string; b: number; }; }'.
3535
!!! error TS2322: Index signatures are incompatible.
3636
!!! error TS2322: Type '{ a: string; b: number; c: string; } | { a: string; b: number; c: number; }' is not assignable to type '{ a: string; b: number; }'.

tests/baselines/reference/assignmentCompatBug2.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/compiler/assignmentCompatBug2.ts(1,5): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
1+
tests/cases/compiler/assignmentCompatBug2.ts(1,27): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
22
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
3-
tests/cases/compiler/assignmentCompatBug2.ts(3,1): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
3+
tests/cases/compiler/assignmentCompatBug2.ts(3,8): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
44
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
5-
tests/cases/compiler/assignmentCompatBug2.ts(5,1): error TS2322: Type '{ b: number; a: number; }' is not assignable to type '{ b: number; }'.
5+
tests/cases/compiler/assignmentCompatBug2.ts(5,13): error TS2322: Type '{ b: number; a: number; }' is not assignable to type '{ b: number; }'.
66
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
77
tests/cases/compiler/assignmentCompatBug2.ts(15,1): error TS2322: Type '{ f: (n: number) => number; g: (s: string) => number; }' is not assignable to type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
88
Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; }'.
@@ -14,17 +14,17 @@ tests/cases/compiler/assignmentCompatBug2.ts(33,1): error TS2322: Type '{ f: (n:
1414

1515
==== tests/cases/compiler/assignmentCompatBug2.ts (6 errors) ====
1616
var b2: { b: number;} = { a: 0 }; // error
17-
~~
17+
~~~~
1818
!!! error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
1919
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
2020

2121
b2 = { a: 0 }; // error
22-
~~
22+
~~~~
2323
!!! error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
2424
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
2525

2626
b2 = {b: 0, a: 0 };
27-
~~
27+
~~~~
2828
!!! error TS2322: Type '{ b: number; a: number; }' is not assignable to type '{ b: number; }'.
2929
!!! error TS2322: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
3030

tests/baselines/reference/assignmentCompatBug5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/assignmentCompatBug5.ts(2,6): error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
1+
tests/cases/compiler/assignmentCompatBug5.ts(2,8): error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
22
Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
33
tests/cases/compiler/assignmentCompatBug5.ts(5,6): error TS2345: Argument of type 'string[]' is not assignable to parameter of type 'number[]'.
44
Type 'string' is not assignable to type 'number'.
@@ -12,7 +12,7 @@ tests/cases/compiler/assignmentCompatBug5.ts(9,6): error TS2345: Argument of typ
1212
==== tests/cases/compiler/assignmentCompatBug5.ts (4 errors) ====
1313
function foo1(x: { a: number; }) { }
1414
foo1({ b: 5 });
15-
~~~~~~~~
15+
~~~~
1616
!!! error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
1717
!!! error TS2345: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
1818

tests/baselines/reference/contextualTyping12.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
tests/cases/compiler/contextualTyping12.ts(1,13): error TS2322: Type '({ id: number; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
1+
tests/cases/compiler/contextualTyping12.ts(1,57): error TS2322: Type '({ id: number; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
22
Type '{ id: number; } | { id: number; name: string; }' is not assignable to type '{ id: number; }'.
33
Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
44
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
55

66

77
==== tests/cases/compiler/contextualTyping12.ts (1 errors) ====
88
class foo { public bar:{id:number;}[] = [{id:1}, {id:2, name:"foo"}]; }
9-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9+
~~~~~~~~~~
1010
!!! error TS2322: Type '({ id: number; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
1111
!!! error TS2322: Type '{ id: number; } | { id: number; name: string; }' is not assignable to type '{ id: number; }'.
1212
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/compiler/contextualTyping17.ts(1,33): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
1+
tests/cases/compiler/contextualTyping17.ts(1,47): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
22
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
33

44

55
==== tests/cases/compiler/contextualTyping17.ts (1 errors) ====
66
var foo: {id:number;} = {id:4}; foo = {id: 5, name:"foo"};
7-
~~~
7+
~~~~~~~~~~
88
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
99
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/compiler/contextualTyping2.ts(1,5): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
1+
tests/cases/compiler/contextualTyping2.ts(1,32): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
22
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
33

44

55
==== tests/cases/compiler/contextualTyping2.ts (1 errors) ====
66
var foo: {id:number;} = {id:4, name:"foo"};
7-
~~~
7+
~~~~~~~~~~
88
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
99
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.

tests/baselines/reference/contextualTyping20.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
tests/cases/compiler/contextualTyping20.ts(1,36): error TS2322: Type '({ id: number; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
1+
tests/cases/compiler/contextualTyping20.ts(1,58): error TS2322: Type '({ id: number; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
22
Type '{ id: number; } | { id: number; name: string; }' is not assignable to type '{ id: number; }'.
33
Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
44
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
55

66

77
==== tests/cases/compiler/contextualTyping20.ts (1 errors) ====
88
var foo:{id:number;}[] = [{id:1}]; foo = [{id:1}, {id:2, name:"foo"}];
9-
~~~
9+
~~~~~~~~~~
1010
!!! error TS2322: Type '({ id: number; } | { id: number; name: string; })[]' is not assignable to type '{ id: number; }[]'.
1111
!!! error TS2322: Type '{ id: number; } | { id: number; name: string; }' is not assignable to type '{ id: number; }'.
1212
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.

0 commit comments

Comments
 (0)