Skip to content

Report excess property errors on object literal names #53129

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 1 commit into from
Mar 7, 2023
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
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20708,9 +20708,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const propDeclaration = prop.valueDeclaration as ObjectLiteralElementLike;
Debug.assertNode(propDeclaration, isObjectLiteralElementLike);

errorNode = propDeclaration;

const name = propDeclaration.name!;
errorNode = name;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this basically uses the same strategy to overwrite the errorNode as the one used by the JSX-related code path here:
https://github.dev/microsoft/TypeScript/blob/6dbec02a88156121c24a152d1e8e5e82d52fdc1d/src/compiler/checker.ts#L20691


if (isIdentifier(name)) {
suggestion = getSuggestionForNonexistentProperty(name, errorTarget);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/arrayCast.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tests/cases/compiler/arrayCast.ts(3,23): error TS2352: Conversion of type '{ foo
// Should fail. Even though the array is contextually typed with { id: number }[], it still
// has type { foo: string }[], which is not assignable to { id: number }[].
<{ id: number; }[]>[{ foo: "s" }];
~~~~~~~~
~~~
!!! error TS2352: Conversion of type '{ foo: string; }[]' to type '{ id: number; }[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
!!! error TS2352: Type '{ foo: string; }' is not comparable to type '{ id: number; }'.
!!! error TS2352: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{

var x1: Action[] = [
{ id: 2, trueness: false },
~~~~~~~~~~~~~~~
~~~~~~~~
!!! error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type 'Action'.
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
{ id: 3, name: "three" }
~~~~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type 'Action'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
]
Expand All @@ -46,11 +46,11 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{
var z1: { id: number }[] =
[
{ id: 2, trueness: false },
~~~~~~~~~~~~~~~
~~~~~~~~
!!! error TS2322: Type '{ id: number; trueness: false; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
{ id: 3, name: "three" }
~~~~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
]
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayLiterals.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,101): erro

// Contextual type C with numeric index signature makes array literal of EveryType E of type BCT(E,C)[]
var context1: { [n: number]: { a: string; b: number; }; } = [{ a: '', b: 0, c: '' }, { a: "", b: 3, c: 0 }];
~~~~~
~
!!! error TS2322: Type '{ a: string; b: number; c: string; }' is not assignable to type '{ a: string; b: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
!!! related TS6501 tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts:24:17: The expected type comes from this index signature.
~~~~
~
!!! error TS2322: Type '{ a: string; b: number; c: number; }' is not assignable to type '{ a: string; b: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
!!! related TS6501 tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts:24:17: The expected type comes from this index signature.
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/assignmentCompatBug2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ tests/cases/compiler/assignmentCompatBug2.ts(33,1): error TS2741: Property 'm' i

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignmentCompatBug5.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tests/cases/compiler/assignmentCompatBug5.ts(9,6): error TS2345: Argument of typ
==== tests/cases/compiler/assignmentCompatBug5.ts (5 errors) ====
function foo1(x: { a: number; }) { }
foo1({ b: 5 });
~~~~
~
!!! error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
!!! error TS2345: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocSatisfiesTag1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

const t1 = /** @satisfies {T1} */ ({ a: 1 });
const t2 = /** @satisfies {T1} */ ({ a: 1, b: 1 });
~~~~
~
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.
const t3 = /** @satisfies {T1} */ ({});
Expand All @@ -45,7 +45,7 @@
const t6 = /** @satisfies {[number, number]} */ ([1, 2]);
const t7 = /** @satisfies {T4} */ ({ a: 'test' });
const t8 = /** @satisfies {T4} */ ({ a: 'test', b: 'test' });
~~~~~~~~~
~
!!! error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T4'.
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T4'.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
a: 0,
b: "hello",
x: 8 // Should error, 'x' isn't in 'Keys'
~~~~
~
!!! error TS1360: Type '{ a: number; b: string; x: number; }' does not satisfy the expected type 'Partial<Record<Keys, unknown>>'.
!!! error TS1360: Object literal may only specify known properties, and 'x' does not exist in type 'Partial<Record<Keys, unknown>>'.
});
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocSatisfiesTag12.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @satisfies {T1}
*/
const t2 = { a: 1, b: 1 };
~~~~
~
!!! error TS1360: Type '{ a: number; b: number; }' does not satisfy the expected type 'T1'.
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T1'.

Expand All @@ -53,7 +53,7 @@
* @satisfies {T2}
*/
const t6 = { a: 'test', b: 'test' };
~~~~~~~~~
~
!!! error TS1360: Type '{ a: string; b: string; }' does not satisfy the expected type 'T2'.
!!! error TS1360: Object literal may only specify known properties, and 'b' does not exist in type 'T2'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/** @satisfies {{ f: (x: string) => string }} */
const t2 = { g: "oops" }; // should error
~~~~~~~~~
~
!!! error TS1360: Type '{ g: string; }' does not satisfy the expected type '{ f: (x: string) => string; }'.
!!! error TS1360: Object literal may only specify known properties, and 'g' does not exist in type '{ f: (x: string) => string; }'.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
a: 0,
b: "hello",
x: 8 // Should error, 'x' isn't in 'Keys'
~~~~
~
!!! error TS1360: Type '{ a: number; b: string; x: number; }' does not satisfy the expected type 'Record<Keys, unknown>'.
!!! error TS1360: Object literal may only specify known properties, and 'x' does not exist in type 'Record<Keys, unknown>'.
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
export const Palette = /** @satisfies {Record<string, Color>} */ ({
white: { r: 255, g: 255, b: 255 },
black: { r: 0, g: 0, d: 0 }, // <- oops! 'd' in place of 'b'
~~~~
~
!!! error TS2322: Type '{ r: number; g: number; d: number; }' is not assignable to type 'Color'.
!!! error TS2322: Object literal may only specify known properties, and 'd' does not exist in type 'Color'.
blue: { r: 0, g: 0, b: 255 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tests/cases/compiler/a.js(8,18): error TS2322: Type '{ c: boolean; }' is not ass

/** @type {Foo} */
export default { c: false };
~~~~~~~~
~
!!! error TS2322: Type '{ c: boolean; }' is not assignable to type 'Foo'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type 'Foo'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tests/cases/compiler/b.js(2,18): error TS2322: Type '{ c: boolean; }' is not ass
==== tests/cases/compiler/b.js (1 errors) ====
/** @type {import("./a").Foo} */
export default { c: false };
~~~~~~~~
~
!!! error TS2322: Type '{ c: boolean; }' is not assignable to type 'Foo'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type 'Foo'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tests/cases/compiler/a.js(8,30): error TS2322: Type '{ a: number; b: number; c:

/** @type {Foo} */
export default { a: 1, b: 1, c: 1 };
~~~~
~
!!! error TS2322: Type '{ a: number; b: number; c: number; }' is not assignable to type 'Foo'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type 'Foo'.

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping12.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ tests/cases/compiler/contextualTyping12.ts(1,57): error TS2322: Type '{ id: numb

==== tests/cases/compiler/contextualTyping12.ts (1 errors) ====
class foo { public bar:{id:number;}[] = [{id:1}, {id:2, name:"foo"}]; }
~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping17.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ tests/cases/compiler/contextualTyping17.ts(1,47): error TS2322: Type '{ id: numb

==== tests/cases/compiler/contextualTyping17.ts (1 errors) ====
var foo: {id:number;} = {id:4}; foo = {id: 5, name:"foo"};
~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ tests/cases/compiler/contextualTyping2.ts(1,32): error TS2322: Type '{ id: numbe

==== tests/cases/compiler/contextualTyping2.ts (1 errors) ====
var foo: {id:number;} = {id:4, name:"foo"};
~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping20.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ tests/cases/compiler/contextualTyping20.ts(1,58): error TS2322: Type '{ id: numb

==== tests/cases/compiler/contextualTyping20.ts (1 errors) ====
var foo:{id:number;}[] = [{id:1}]; foo = [{id:1}, {id:2, name:"foo"}];
~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping4.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ tests/cases/compiler/contextualTyping4.ts(1,46): error TS2322: Type '{ id: numbe

==== tests/cases/compiler/contextualTyping4.ts (1 errors) ====
class foo { public bar:{id:number;} = {id:5, name:"foo"}; }
~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping9.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ tests/cases/compiler/contextualTyping9.ts(1,42): error TS2322: Type '{ id: numbe

==== tests/cases/compiler/contextualTyping9.ts (1 errors) ====
var foo:{id:number;}[] = [{id:1}, {id:2, name:"foo"}];
~~~~~~~~~~
~~~~
!!! error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts(27,34
}

TestComponent({icon: { props: { INVALID_PROP_NAME: 'share', ariaLabel: 'test label' } }});
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'.
!!! error TS2322: Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'.
!!! related TS6500 tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts:14:3: The expected type comes from property 'props' which is declared here on type 'NestedProp<ITestProps>'
Expand All @@ -36,7 +36,7 @@ tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts(27,34
}

TestComponent2({icon: { props: { INVALID_PROP_NAME: 'share', ariaLabel: 'test label' } }});
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ INVALID_PROP_NAME: string; ariaLabel: string; }' is not assignable to type 'ITestProps'.
!!! error TS2322: Object literal may only specify known properties, and 'INVALID_PROP_NAME' does not exist in type 'ITestProps'.
!!! related TS6500 tests/cases/compiler/deepExcessPropertyCheckingWhenTargetIsIntersection.ts:14:3: The expected type comes from property 'props' which is declared here on type 'NestedProp<ITestProps>'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tests/cases/conformance/classes/propertyMemberDeclarations/memberFunctionDeclara
bar() {
var r = super.foo({ a: 1 }); // { a: number }
var r2 = super.foo({ a: 1, b: 2 }); // { a: number }
~~~~
~
!!! error TS2345: Argument of type '{ a: number; b: number; }' is not assignable to parameter of type '{ a: number; }'.
!!! error TS2345: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
var r3 = this.foo({ a: 1, b: 2 }); // { a: number; b: number; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterProperties5.ts(1
}

var a = new C1([{ x1: 10, x2: "", x3: true }, "", false]);
~~~~~~
~~
!!! error TS2322: Type '{ x1: number; x2: string; x3: true; }' is not assignable to type 'ObjType1'.
!!! error TS2322: Object literal may only specify known properties, and 'x1' does not exist in type 'ObjType1'.
~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tests/cases/compiler/discriminatedUnionErrorMessage.ts(10,5): error TS2322: Type
let shape: Shape = {
kind: "sq",
x: 12,
~~~~~
~
!!! error TS2322: Type '{ kind: "sq"; x: number; y: number; }' is not assignable to type 'Shape'.
!!! error TS2322: Object literal may only specify known properties, and 'x' does not exist in type 'Square'.
y: 13,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS

function f13(x: { a: null; b: string } | { a: string, c: number }) {
x = { a: null, b: "foo", c: 4}; // Error
~~~~
~
!!! error TS2322: Type '{ a: null; b: string; c: number; }' is not assignable to type '{ a: null; b: string; } | { a: string; c: number; }'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: null; b: string; }'.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(47,10): error TS234

g(x);
g({ a: '', b: '' })
~~~~~
~
!!! error TS2345: Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'Bar | Other'.
!!! error TS2345: Object literal may only specify known properties, and 'a' does not exist in type 'Bar | Other'.

Expand Down Expand Up @@ -80,7 +80,7 @@ tests/cases/compiler/errorsOnUnionsOfOverlappingObjects01.ts(47,10): error TS234

const manBeer = { man: "Manny", beer: "Coffee" };
addToZoo({ man: "Manny", beer: "Coffee" });
~~~~~~~~~~~~~~
~~~~
!!! error TS2345: Argument of type '{ man: string; beer: string; }' is not assignable to parameter of type 'ExoticAnimal'.
!!! error TS2345: Object literal may only specify known properties, and 'beer' does not exist in type 'ExoticAnimal'.
addToZoo(manBeer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tests/cases/compiler/excessPropertyCheckIntersectionWithIndexSignature.ts(7,24):
!!! related TS2728 tests/cases/compiler/excessPropertyCheckIntersectionWithIndexSignature.ts:3:53: 'b' is declared here.
x = { y: { a: 0, b: 0 } };
x = { y: { a: 0, b: 0, c: 0 } }; // Error
~~~~
~
!!! error TS2322: Type '{ a: 0; b: 0; c: number; }' is not assignable to type '{ a: 0; } & { b: 0; }'.
!!! error TS2322: Object literal may only specify known properties, and 'c' does not exist in type '{ a: 0; } & { b: 0; }'.

Expand Down
Loading