Skip to content

Fixes #35735: Avoids listing missing properties for types with only call/construct signatures #40973

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
Mar 4, 2022
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
16 changes: 15 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19996,7 +19996,7 @@ namespace ts {
const requireOptionalProperties = (relation === subtypeRelation || relation === strictSubtypeRelation) && !isObjectLiteralType(source) && !isEmptyArrayLiteralType(source) && !isTupleType(source);
const unmatchedProperty = getUnmatchedProperty(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false);
if (unmatchedProperty) {
if (reportErrors) {
if (reportErrors && shouldReportUnmatchedPropertyError(source, target)) {
reportUnmatchedProperty(source, target, unmatchedProperty, requireOptionalProperties);
}
return Ternary.False;
Expand Down Expand Up @@ -20154,6 +20154,20 @@ namespace ts {
return result;
}

function shouldReportUnmatchedPropertyError(source: Type, target: Type): boolean {
const typeCallSignatures = getSignaturesOfStructuredType(source, SignatureKind.Call);
const typeConstructSignatures = getSignaturesOfStructuredType(source, SignatureKind.Construct);
const typeProperties = getPropertiesOfObjectType(source);
if ((typeCallSignatures.length || typeConstructSignatures.length) && !typeProperties.length) {
if ((getSignaturesOfType(target, SignatureKind.Call).length && typeCallSignatures.length) ||
(getSignaturesOfType(target, SignatureKind.Construct).length && typeConstructSignatures.length)) {
return true; // target has similar signature kinds to source, still focus on the unmatched property
}
return false;
}
return true;
}

function reportIncompatibleCallSignatureReturn(siga: Signature, sigb: Signature) {
if (siga.parameters.length === 0 && sigb.parameters.length === 0) {
return (source: Type, target: Type) => reportIncompatibleError(Diagnostics.Call_signatures_with_no_arguments_have_incompatible_return_types_0_and_1, typeToString(source), typeToString(target));
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayAssignmentTest1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tests/cases/compiler/arrayAssignmentTest1.ts(76,1): error TS2322: Type 'C1[]' is
Property 'CM3M1' is missing in type 'C1' but required in type 'C3'.
tests/cases/compiler/arrayAssignmentTest1.ts(77,1): error TS2322: Type 'I1[]' is not assignable to type 'C3[]'.
Property 'CM3M1' is missing in type 'I1' but required in type 'C3'.
tests/cases/compiler/arrayAssignmentTest1.ts(79,1): error TS2740: Type '() => C1' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
tests/cases/compiler/arrayAssignmentTest1.ts(79,1): error TS2322: Type '() => C1' is not assignable to type 'any[]'.
tests/cases/compiler/arrayAssignmentTest1.ts(80,1): error TS2740: Type '{ one: number; }' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
tests/cases/compiler/arrayAssignmentTest1.ts(82,1): error TS2740: Type 'C1' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
tests/cases/compiler/arrayAssignmentTest1.ts(83,1): error TS2740: Type 'C2' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
Expand Down Expand Up @@ -152,7 +152,7 @@ tests/cases/compiler/arrayAssignmentTest1.ts(85,1): error TS2740: Type 'I1' is m

arr_any = f1; // should be an error - is
~~~~~~~
!!! error TS2740: Type '() => C1' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
!!! error TS2322: Type '() => C1' is not assignable to type 'any[]'.
arr_any = o1; // should be an error - is
~~~~~~~
!!! error TS2740: Type '{ one: number; }' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/arrayAssignmentTest2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ tests/cases/compiler/arrayAssignmentTest2.ts(48,1): error TS2322: Type 'C1[]' is
Property 'CM3M1' is missing in type 'C1' but required in type 'C3'.
tests/cases/compiler/arrayAssignmentTest2.ts(49,1): error TS2322: Type 'I1[]' is not assignable to type 'C3[]'.
Property 'CM3M1' is missing in type 'I1' but required in type 'C3'.
tests/cases/compiler/arrayAssignmentTest2.ts(51,1): error TS2740: Type '() => C1' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
tests/cases/compiler/arrayAssignmentTest2.ts(52,1): error TS2740: Type '() => any' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
tests/cases/compiler/arrayAssignmentTest2.ts(51,1): error TS2322: Type '() => C1' is not assignable to type 'any[]'.
tests/cases/compiler/arrayAssignmentTest2.ts(52,1): error TS2322: Type '() => any' is not assignable to type 'any[]'.
tests/cases/compiler/arrayAssignmentTest2.ts(53,1): error TS2740: Type '{ one: number; }' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
tests/cases/compiler/arrayAssignmentTest2.ts(55,1): error TS2740: Type 'C1' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
tests/cases/compiler/arrayAssignmentTest2.ts(56,1): error TS2740: Type 'C2' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
Expand Down Expand Up @@ -78,10 +78,10 @@ tests/cases/compiler/arrayAssignmentTest2.ts(58,1): error TS2740: Type 'I1' is m

arr_any = f1; // should be an error - is
~~~~~~~
!!! error TS2740: Type '() => C1' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
!!! error TS2322: Type '() => C1' is not assignable to type 'any[]'.
arr_any = function () { return null;} // should be an error - is
~~~~~~~
!!! error TS2740: Type '() => any' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
!!! error TS2322: Type '() => any' is not assignable to type 'any[]'.
arr_any = o1; // should be an error - is
~~~~~~~
!!! error TS2740: Type '{ one: number; }' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayAssignmentTest4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/arrayAssignmentTest4.ts(22,1): error TS2740: Type '() => any' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
tests/cases/compiler/arrayAssignmentTest4.ts(22,1): error TS2322: Type '() => any' is not assignable to type 'any[]'.
tests/cases/compiler/arrayAssignmentTest4.ts(23,1): error TS2740: Type 'C3' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.


Expand Down Expand Up @@ -26,7 +26,7 @@ tests/cases/compiler/arrayAssignmentTest4.ts(23,1): error TS2740: Type 'C3' is m

arr_any = function () { return null;} // should be an error - is
~~~~~~~
!!! error TS2740: Type '() => any' is missing the following properties from type 'any[]': pop, push, concat, join, and 15 more.
!!! error TS2322: Type '() => any' is not assignable to type 'any[]'.
arr_any = c3; // should be an error - is
~~~~~~~
!!! error TS2740: Type 'C3' is missing the following properties from type 'any[]': length, pop, push, concat, and 16 more.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tests/cases/compiler/assignLambdaToNominalSubtypeOfFunction.ts(7,4): error TS2345: Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type 'IResultCallback'.
Property 'x' is missing in type '(a: any, b: any) => boolean' but required in type 'IResultCallback'.
tests/cases/compiler/assignLambdaToNominalSubtypeOfFunction.ts(8,4): error TS2345: Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type 'IResultCallback'.
Property 'x' is missing in type '(a: any, b: any) => boolean' but required in type 'IResultCallback'.


==== tests/cases/compiler/assignLambdaToNominalSubtypeOfFunction.ts (2 errors) ====
Expand All @@ -14,11 +12,7 @@ tests/cases/compiler/assignLambdaToNominalSubtypeOfFunction.ts(8,4): error TS234
fn((a, b) => true);
~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type 'IResultCallback'.
!!! error TS2345: Property 'x' is missing in type '(a: any, b: any) => boolean' but required in type 'IResultCallback'.
!!! related TS2728 tests/cases/compiler/assignLambdaToNominalSubtypeOfFunction.ts:2:5: 'x' is declared here.
fn(function (a, b) { return true; })
~~~~~~~~
!!! error TS2345: Argument of type '(a: any, b: any) => boolean' is not assignable to parameter of type 'IResultCallback'.
!!! error TS2345: Property 'x' is missing in type '(a: any, b: any) => boolean' but required in type 'IResultCallback'.
!!! related TS2728 tests/cases/compiler/assignLambdaToNominalSubtypeOfFunction.ts:2:5: 'x' is declared here.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(31,1): error TS2741: Property 'f' is missing in type '() => number' but required in type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(32,1): error TS2741: Property 'f' is missing in type '(x: number) => string' but required in type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(33,1): error TS2741: Property 'f' is missing in type '() => number' but required in type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(34,1): error TS2741: Property 'f' is missing in type '(x: number) => string' but required in type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(31,1): error TS2322: Type '() => number' is not assignable to type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(32,1): error TS2322: Type '(x: number) => string' is not assignable to type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(33,1): error TS2322: Type '() => number' is not assignable to type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(34,1): error TS2322: Type '(x: number) => string' is not assignable to type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(42,1): error TS2322: Type 'S2' is not assignable to type 'T'.
Types of property 'f' are incompatible.
Type '(x: string) => void' is not assignable to type '(x: number) => void'.
Expand All @@ -12,8 +12,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
Type '(x: string) => void' is not assignable to type '(x: number) => void'.
Types of parameters 'x' and 'x' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(44,1): error TS2741: Property 'f' is missing in type '(x: string) => number' but required in type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(45,1): error TS2741: Property 'f' is missing in type '(x: string) => string' but required in type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(44,1): error TS2322: Type '(x: string) => number' is not assignable to type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(45,1): error TS2322: Type '(x: string) => string' is not assignable to type 'T'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(46,1): error TS2322: Type 'S2' is not assignable to type '{ f(x: number): void; }'.
Types of property 'f' are incompatible.
Type '(x: string) => void' is not assignable to type '(x: number) => void'.
Expand All @@ -24,8 +24,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
Type '(x: string) => void' is not assignable to type '(x: number) => void'.
Types of parameters 'x' and 'x' are incompatible.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(48,1): error TS2741: Property 'f' is missing in type '(x: string) => number' but required in type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(49,1): error TS2741: Property 'f' is missing in type '(x: string) => string' but required in type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(48,1): error TS2322: Type '(x: string) => number' is not assignable to type '{ f(x: number): void; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts(49,1): error TS2322: Type '(x: string) => string' is not assignable to type '{ f(x: number): void; }'.


==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts (12 errors) ====
Expand Down Expand Up @@ -61,20 +61,16 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
// errors
t = () => 1;
~
!!! error TS2741: Property 'f' is missing in type '() => number' but required in type 'T'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:4:5: 'f' is declared here.
!!! error TS2322: Type '() => number' is not assignable to type 'T'.
t = function (x: number) { return ''; }
~
!!! error TS2741: Property 'f' is missing in type '(x: number) => string' but required in type 'T'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:4:5: 'f' is declared here.
!!! error TS2322: Type '(x: number) => string' is not assignable to type 'T'.
a = () => 1;
~
!!! error TS2741: Property 'f' is missing in type '() => number' but required in type '{ f(x: number): void; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:7:10: 'f' is declared here.
!!! error TS2322: Type '() => number' is not assignable to type '{ f(x: number): void; }'.
a = function (x: number) { return ''; }
~
!!! error TS2741: Property 'f' is missing in type '(x: number) => string' but required in type '{ f(x: number): void; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:7:10: 'f' is declared here.
!!! error TS2322: Type '(x: number) => string' is not assignable to type '{ f(x: number): void; }'.

interface S2 {
f(x: string): void;
Expand All @@ -98,12 +94,10 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
!!! error TS2322: Type 'number' is not assignable to type 'string'.
t = (x: string) => 1;
~
!!! error TS2741: Property 'f' is missing in type '(x: string) => number' but required in type 'T'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:4:5: 'f' is declared here.
!!! error TS2322: Type '(x: string) => number' is not assignable to type 'T'.
t = function (x: string) { return ''; }
~
!!! error TS2741: Property 'f' is missing in type '(x: string) => string' but required in type 'T'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:4:5: 'f' is declared here.
!!! error TS2322: Type '(x: string) => string' is not assignable to type 'T'.
a = s2;
~
!!! error TS2322: Type 'S2' is not assignable to type '{ f(x: number): void; }'.
Expand All @@ -120,10 +114,8 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
!!! error TS2322: Type 'number' is not assignable to type 'string'.
a = (x: string) => 1;
~
!!! error TS2741: Property 'f' is missing in type '(x: string) => number' but required in type '{ f(x: number): void; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:7:10: 'f' is declared here.
!!! error TS2322: Type '(x: string) => number' is not assignable to type '{ f(x: number): void; }'.
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this one is super subtle! But maybe there's room for a follow-up bug.

a = function (x: string) { return ''; }
~
!!! error TS2741: Property 'f' is missing in type '(x: string) => string' but required in type '{ f(x: number): void; }'.
!!! related TS2728 tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures2.ts:7:10: 'f' is declared here.
!!! error TS2322: Type '(x: string) => string' is not assignable to type '{ f(x: number): void; }'.

Loading