Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 26ab0cd

Browse filesBrowse files
authored
Merge pull request microsoft#16530 from Microsoft/excess-property-check-error-span-for-spread-property
Improve excess property check error span for spread property
2 parents 187febd + ef70a6c commit 26ab0cd
Copy full SHA for 26ab0cd

4 files changed

+55-2Lines changed: 55 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8955,7 +8955,9 @@ namespace ts {
89558955
reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(target));
89568956
}
89578957
else {
8958-
if (prop.valueDeclaration) {
8958+
// use the property's value declaration if the property is assigned inside the literal itself
8959+
const objectLiteralDeclaration = source.symbol && firstOrUndefined(source.symbol.declarations);
8960+
if (prop.valueDeclaration && findAncestor(prop.valueDeclaration, d => d === objectLiteralDeclaration)) {
89598961
errorNode = prop.valueDeclaration;
89608962
}
89618963
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
Collapse file

‎tests/baselines/reference/objectSpreadNegative.errors.txt‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/objectSpreadNegative.errors.txt
+26-1Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ tests/cases/conformance/types/spread/objectSpreadNegative.ts(52,9): error TS2339
1717
tests/cases/conformance/types/spread/objectSpreadNegative.ts(57,11): error TS2339: Property 'a' does not exist on type '{}'.
1818
tests/cases/conformance/types/spread/objectSpreadNegative.ts(61,14): error TS2698: Spread types may only be created from object types.
1919
tests/cases/conformance/types/spread/objectSpreadNegative.ts(64,14): error TS2698: Spread types may only be created from object types.
20+
tests/cases/conformance/types/spread/objectSpreadNegative.ts(78,37): error TS2322: Type '{ a: string; b: string; extra: string; }' is not assignable to type 'A'.
21+
Object literal may only specify known properties, and 'extra' does not exist in type 'A'.
22+
tests/cases/conformance/types/spread/objectSpreadNegative.ts(81,7): error TS2322: Type '{ a: string; b: string; extra: string; }' is not assignable to type 'A'.
23+
Object literal may only specify known properties, and 'extra' does not exist in type 'A'.
24+
tests/cases/conformance/types/spread/objectSpreadNegative.ts(83,7): error TS2322: Type '{ a: string; b: string; extra: string; }' is not assignable to type 'A'.
25+
Object literal may only specify known properties, and 'extra' does not exist in type 'A'.
2026

2127

22-
==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (16 errors) ====
28+
==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (19 errors) ====
2329
let o = { a: 1, b: 'no' }
2430

2531
/// private propagates
@@ -128,4 +134,23 @@ tests/cases/conformance/types/spread/objectSpreadNegative.ts(64,14): error TS269
128134
f({ a: 1 }, { a: 'mismatch' })
129135
let overwriteId: { id: string, a: number, c: number, d: string } =
130136
f({ a: 1, id: true }, { c: 1, d: 'no' })
137+
138+
// excess property checks
139+
type A = { a: string, b: string };
140+
type Extra = { a: string, b: string, extra: string };
141+
const extra1: A = { a: "a", b: "b", extra: "extra" };
142+
~~~~~~~~~~~~~~
143+
!!! error TS2322: Type '{ a: string; b: string; extra: string; }' is not assignable to type 'A'.
144+
!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'A'.
145+
const extra2 = { a: "a", b: "b", extra: "extra" };
146+
const a1: A = { ...extra1 }; // error spans should be here
147+
const a2: A = { ...extra2 }; // not on the symbol declarations above
148+
~~
149+
!!! error TS2322: Type '{ a: string; b: string; extra: string; }' is not assignable to type 'A'.
150+
!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'A'.
151+
const extra3: Extra = { a: "a", b: "b", extra: "extra" };
152+
const a3: A = { ...extra3 }; // same here
153+
~~
154+
!!! error TS2322: Type '{ a: string; b: string; extra: string; }' is not assignable to type 'A'.
155+
!!! error TS2322: Object literal may only specify known properties, and 'extra' does not exist in type 'A'.
131156

Collapse file

‎tests/baselines/reference/objectSpreadNegative.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/objectSpreadNegative.js
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ let overlapConflict: { id:string, a: string } =
7272
f({ a: 1 }, { a: 'mismatch' })
7373
let overwriteId: { id: string, a: number, c: number, d: string } =
7474
f({ a: 1, id: true }, { c: 1, d: 'no' })
75+
76+
// excess property checks
77+
type A = { a: string, b: string };
78+
type Extra = { a: string, b: string, extra: string };
79+
const extra1: A = { a: "a", b: "b", extra: "extra" };
80+
const extra2 = { a: "a", b: "b", extra: "extra" };
81+
const a1: A = { ...extra1 }; // error spans should be here
82+
const a2: A = { ...extra2 }; // not on the symbol declarations above
83+
const extra3: Extra = { a: "a", b: "b", extra: "extra" };
84+
const a3: A = { ...extra3 }; // same here
7585

7686

7787
//// [objectSpreadNegative.js]
@@ -152,3 +162,9 @@ var exclusive = f({ a: 1, b: 'yes' }, { c: 'no', d: false });
152162
var overlap = f({ a: 1 }, { a: 2, b: 'extra' });
153163
var overlapConflict = f({ a: 1 }, { a: 'mismatch' });
154164
var overwriteId = f({ a: 1, id: true }, { c: 1, d: 'no' });
165+
var extra1 = { a: "a", b: "b", extra: "extra" };
166+
var extra2 = { a: "a", b: "b", extra: "extra" };
167+
var a1 = __assign({}, extra1); // error spans should be here
168+
var a2 = __assign({}, extra2); // not on the symbol declarations above
169+
var extra3 = { a: "a", b: "b", extra: "extra" };
170+
var a3 = __assign({}, extra3); // same here
Collapse file

‎tests/cases/conformance/types/spread/objectSpreadNegative.ts‎

Copy file name to clipboardExpand all lines: tests/cases/conformance/types/spread/objectSpreadNegative.ts
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ let overlapConflict: { id:string, a: string } =
7272
f({ a: 1 }, { a: 'mismatch' })
7373
let overwriteId: { id: string, a: number, c: number, d: string } =
7474
f({ a: 1, id: true }, { c: 1, d: 'no' })
75+
76+
// excess property checks
77+
type A = { a: string, b: string };
78+
type Extra = { a: string, b: string, extra: string };
79+
const extra1: A = { a: "a", b: "b", extra: "extra" };
80+
const extra2 = { a: "a", b: "b", extra: "extra" };
81+
const a1: A = { ...extra1 }; // error spans should be here
82+
const a2: A = { ...extra2 }; // not on the symbol declarations above
83+
const extra3: Extra = { a: "a", b: "b", extra: "extra" };
84+
const a3: A = { ...extra3 }; // same here

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.