Skip to content

Skip excess property elaborations #29760

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

Closed
wants to merge 6 commits into from
Closed
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
16 changes: 15 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11886,6 +11886,11 @@ namespace ts {
let expandingFlags = ExpandingFlags.None;
let overflow = false;
let suppressNextError = false;
// Ordinarily, we want to construct "breadcrumbs" at each level as we dive into types
// so that the user can build up the same context that the relationship checker had and
// diagnose issues; however, sometimes this trail is just noise when the deepest elaboration
// is already so specific and obvious. In some cases, we want to just bubble up a single diagnostic.
let shouldConstructDiagnosticTrail = true;

Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");

Expand Down Expand Up @@ -11928,7 +11933,9 @@ namespace ts {

function reportError(message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): void {
Debug.assert(!!errorNode);
errorInfo = chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2, arg3);
if (shouldConstructDiagnosticTrail) {
errorInfo = chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2, arg3);
}
}

function associateRelatedInfo(info: DiagnosticRelatedInformation) {
Expand Down Expand Up @@ -12243,6 +12250,13 @@ namespace ts {
reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
symbolToString(prop), typeToString(errorTarget));
}

// Signal to the rest of the relationship check not to provide the trail of context.
// We don't want to overwhelm our users with the context, and hopefully they
// already have enough information to piece this together.
// NOTE: don't move this closer to the re-assignment of `errorNode` since
// this needs to happen *after* reporting errors at this level.
shouldConstructDiagnosticTrail = false;
}
}
return true;
Expand Down
12 changes: 6 additions & 6 deletions src/testRunner/unittests/tscWatch/emitAndErrorUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ getPoint().c.x;`
change: host => host.writeFile(aFile.path, aFile.content.replace("x2", "x")),
getInitialErrors: watch => [
getDiagnosticOfFileFromProgram(watch(), cFile.path, cFile.content.indexOf("x: 1"), 4, chainDiagnosticMessages(
chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, "x", "Coords"),
Diagnostics.Type_0_is_not_assignable_to_type_1,
"{ x: number; y: number; }",
"Coords"
)),
/*details*/ undefined,
Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1,
"x",
"Coords"),
),
getDiagnosticOfFileFromProgram(watch(), dFile.path, dFile.content.lastIndexOf("x"), 1, Diagnostics.Property_0_does_not_exist_on_type_1, "x", "Coords")
],
getIncrementalErrors: () => emptyArray
Expand Down Expand Up @@ -272,7 +272,7 @@ export class Data {
change: host => host.writeFile(lib1ToolsInterface.path, lib1ToolsInterface.content.replace("title", "title2")),
getInitialErrors: () => emptyArray,
getIncrementalErrors: () => [
"lib2/data.ts(5,13): error TS2322: Type '{ title: string; }' is not assignable to type 'ITest'.\n Object literal may only specify known properties, but 'title' does not exist in type 'ITest'. Did you mean to write 'title2'?\n"
"lib2/data.ts(5,13): error TS2561: Object literal may only specify known properties, but 'title' does not exist in type 'ITest'. Did you mean to write 'title2'?\n"
]
});
}
Expand Down
8 changes: 2 additions & 6 deletions tests/baselines/reference/arrayCast.errors.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
tests/cases/compiler/arrayCast.ts(3,23): 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.
Type '{ foo: string; }' is not comparable to type '{ id: number; }'.
Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.
tests/cases/compiler/arrayCast.ts(3,23): error TS2353: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.


==== tests/cases/compiler/arrayCast.ts (1 errors) ====
// 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; }'.
!!! error TS2353: Object literal may only specify known properties, and 'foo' does not exist in type '{ id: number; }'.

// Should succeed, as the {} element causes the type of the array to be {}[]
<{ id: number; }[]>[{ foo: "s" }, {}];
24 changes: 8 additions & 16 deletions tests/baselines/reference/arrayLiteralTypeInference.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
tests/cases/compiler/arrayLiteralTypeInference.ts(14,14): error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type 'Action'.
Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(15,14): error TS2322: Type '{ id: number; name: string; }' is not assignable to type 'Action'.
Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(31,18): error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type '{ id: number; }'.
Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
tests/cases/compiler/arrayLiteralTypeInference.ts(14,14): error TS2353: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(15,14): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
tests/cases/compiler/arrayLiteralTypeInference.ts(31,18): error TS2353: Object literal may only specify known properties, and 'trueness' does not exist in type '{ id: number; }'.
tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.


==== tests/cases/compiler/arrayLiteralTypeInference.ts (4 errors) ====
Expand All @@ -24,12 +20,10 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{
var x1: Action[] = [
{ id: 2, trueness: false },
~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ id: number; trueness: boolean; }' is not assignable to type 'Action'.
!!! error TS2322: Object literal may only specify known properties, and 'trueness' does not exist in type 'Action'.
!!! error TS2353: 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'.
!!! error TS2353: Object literal may only specify known properties, and 'name' does not exist in type 'Action'.
]

var x2: Action[] = [
Expand All @@ -47,12 +41,10 @@ tests/cases/compiler/arrayLiteralTypeInference.ts(32,18): error TS2322: Type '{
[
{ id: 2, trueness: false },
~~~~~~~~~~~~~~~
!!! error TS2322: Type '{ id: number; trueness: boolean; }' 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; }'.
!!! error TS2353: 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; }'.
!!! error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
]

var z2: { id: number }[] =
Expand Down
12 changes: 4 additions & 8 deletions tests/baselines/reference/arrayLiterals.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,77): error TS2322: Type '{ a: string; b: number; c: string; }' is not assignable to type '{ a: string; b: number; }'.
Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,101): error TS2322: Type '{ a: string; b: number; c: number; }' is not assignable to type '{ a: string; b: number; }'.
Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,77): error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.
tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts(24,101): error TS2353: Object literal may only specify known properties, and 'c' does not exist in type '{ a: string; b: number; }'.


==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals.ts (2 errors) ====
Expand Down Expand Up @@ -30,12 +28,10 @@ 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; }'.
!!! error TS2353: 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; }'.
!!! error TS2353: 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.
var context2 = [{ a: '', b: 0, c: '' }, { a: "", b: 3, c: 0 }];

Expand Down
18 changes: 6 additions & 12 deletions tests/baselines/reference/assignmentCompatBug2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
tests/cases/compiler/assignmentCompatBug2.ts(1,27): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
tests/cases/compiler/assignmentCompatBug2.ts(3,8): error TS2322: Type '{ a: number; }' is not assignable to type '{ b: number; }'.
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
tests/cases/compiler/assignmentCompatBug2.ts(5,13): error TS2322: Type '{ b: number; a: number; }' is not assignable to type '{ b: number; }'.
Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
tests/cases/compiler/assignmentCompatBug2.ts(1,27): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
tests/cases/compiler/assignmentCompatBug2.ts(3,8): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
tests/cases/compiler/assignmentCompatBug2.ts(5,13): error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.
tests/cases/compiler/assignmentCompatBug2.ts(15,1): error TS2741: Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; }' but required in type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
tests/cases/compiler/assignmentCompatBug2.ts(20,1): error TS2741: Property 'g' is missing in type '{ f: (n: number) => number; m: number; }' but required in type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
tests/cases/compiler/assignmentCompatBug2.ts(33,1): error TS2741: Property 'm' is missing in type '{ f: (n: number) => number; g: (s: string) => number; n: number; k: (a: any) => any; }' but required in type '{ f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; }'.
Expand All @@ -12,18 +9,15 @@ 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; }'.
!!! error TS2353: 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; }'.
!!! error TS2353: 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; }'.
!!! error TS2353: Object literal may only specify known properties, and 'a' does not exist in type '{ b: number; }'.

var b3: { f(n: number): number; g(s: string): number; m: number; n?: number; k?(a: any): any; };

Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/assignmentCompatBug5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tests/cases/compiler/assignmentCompatBug5.ts(2,8): error TS2345: Argument of type '{ b: number; }' is not assignable to parameter of type '{ a: number; }'.
Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
tests/cases/compiler/assignmentCompatBug5.ts(2,8): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.
tests/cases/compiler/assignmentCompatBug5.ts(5,7): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/compiler/assignmentCompatBug5.ts(5,12): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/compiler/assignmentCompatBug5.ts(8,6): error TS2345: Argument of type '(s: string) => void' is not assignable to parameter of type '(n: number) => number'.
Expand All @@ -13,8 +12,7 @@ tests/cases/compiler/assignmentCompatBug5.ts(9,6): error TS2345: Argument of typ
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; }'.
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type '{ a: number; }'.

function foo2(x: number[]) { }
foo2(["s", "t"]);
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/contextualTyping12.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
tests/cases/compiler/contextualTyping12.ts(1,57): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
tests/cases/compiler/contextualTyping12.ts(1,57): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.


==== 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; }'.
!!! error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
6 changes: 2 additions & 4 deletions tests/baselines/reference/contextualTyping17.errors.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
tests/cases/compiler/contextualTyping17.ts(1,47): error TS2322: Type '{ id: number; name: string; }' is not assignable to type '{ id: number; }'.
Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
tests/cases/compiler/contextualTyping17.ts(1,47): error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.


==== 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; }'.
!!! error TS2353: Object literal may only specify known properties, and 'name' does not exist in type '{ id: number; }'.
Loading