Skip to content

Hide function type missing props if return and target type same. #36746

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 1 commit 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
12 changes: 7 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14074,8 +14074,8 @@ namespace ts {
source: Type,
target: Type,
relation: Map<RelationComparisonResult>,
headMessage: DiagnosticMessage | undefined,
containingMessageChain: (() => DiagnosticMessageChain | undefined) | undefined,
_headMessage: DiagnosticMessage | undefined,
Copy link
Member

Choose a reason for hiding this comment

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

If these parameters are unused, personally, I'd remove them.

_containingMessageChain: (() => DiagnosticMessageChain | undefined) | undefined,
errorOutputContainer: { errors?: Diagnostic[], skipLogging?: boolean } | undefined
): boolean {
const callSignatures = getSignaturesOfType(source, SignatureKind.Call);
Expand All @@ -14085,13 +14085,15 @@ namespace ts {
const returnType = getReturnTypeOfSignature(s);
return !(returnType.flags & (TypeFlags.Any | TypeFlags.Never)) && checkTypeRelatedTo(returnType, target, relation, /*errorNode*/ undefined);
})) {
const resultObj: { errors?: Diagnostic[] } = errorOutputContainer || {};
checkTypeAssignableTo(source, target, node, headMessage, containingMessageChain, resultObj);
const diagnostic = resultObj.errors![resultObj.errors!.length - 1];
const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target);
const diagnostic = error(node, Diagnostics.Type_0_is_not_assignable_to_type_1, sourceType, targetType);
addRelatedInfo(diagnostic, createDiagnosticForNode(
node,
signatures === constructSignatures ? Diagnostics.Did_you_mean_to_use_new_with_this_expression : Diagnostics.Did_you_mean_to_call_this_expression
));
if (errorOutputContainer && errorOutputContainer.skipLogging) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not really familiar with errorOutputContainer, but it sort of looks like this should have an else case that adds the diagnostic to diagnostics instead. @sandersn should be able to confirm.

Copy link
Member

Choose a reason for hiding this comment

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

In the else clause, you'd add the diagnostic to the headMessage or containingMessageChain, which is was checkTypeAssignableTo does. The logic there is somewhat complicated so I'm not sure it's a good idea to replicate it here either. You still need to call checkTypeAssignableTo.

(errorOutputContainer.errors || (errorOutputContainer.errors = [])).push(diagnostic);
}
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
tests/cases/conformance/jsx/file.tsx(24,28): error TS2551: Property 'NAme' does not exist on type 'IUser'. Did you mean 'Name'?
tests/cases/conformance/jsx/file.tsx(36,15): error TS2322: Type '(user: IUser) => Element' is not assignable to type 'string | number | boolean | any[] | ReactElement<any>'.
Copy link
Member

Choose a reason for hiding this comment

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

Wasn't there supposed to be some new text about "did you mean to call?"? Or does that not apply in this case?

Type '(user: IUser) => Element' is missing the following properties from type 'ReactElement<any>': type, props
tests/cases/conformance/jsx/file.tsx(39,15): error TS2322: Type '(user: IUser) => Element' is not assignable to type 'string | number | boolean | any[] | ReactElement<any>'.
Type '(user: IUser) => Element' is missing the following properties from type 'ReactElement<any>': type, props


==== tests/cases/conformance/jsx/file.tsx (3 errors) ====
Expand Down Expand Up @@ -51,7 +49,6 @@ tests/cases/conformance/jsx/file.tsx(39,15): error TS2322: Type '(user: IUser) =
) }
~~~~~~~~~~~~~
!!! error TS2322: Type '(user: IUser) => Element' is not assignable to type 'string | number | boolean | any[] | ReactElement<any>'.
!!! error TS2322: Type '(user: IUser) => Element' is missing the following properties from type 'ReactElement<any>': type, props
!!! related TS6212 tests/cases/conformance/jsx/file.tsx:36:15: Did you mean to call this expression?
{ user => (
~~~~~~~~~
Expand All @@ -60,7 +57,6 @@ tests/cases/conformance/jsx/file.tsx(39,15): error TS2322: Type '(user: IUser) =
) }
~~~~~~~~~~~~~
!!! error TS2322: Type '(user: IUser) => Element' is not assignable to type 'string | number | boolean | any[] | ReactElement<any>'.
!!! error TS2322: Type '(user: IUser) => Element' is missing the following properties from type 'ReactElement<any>': type, props
!!! related TS6212 tests/cases/conformance/jsx/file.tsx:39:15: Did you mean to call this expression?
</FetchUser>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/jsx/file.tsx(20,10): error TS2741: Property 'children' is missing in type '{ a: number; b: string; }' but required in type 'Prop'.
tests/cases/conformance/jsx/file.tsx(25,9): error TS2740: Type 'Element' is missing the following properties from type 'Button': render, setState, forceUpdate, state, and 2 more.
tests/cases/conformance/jsx/file.tsx(29,10): error TS2740: Type 'typeof Button' is missing the following properties from type 'Button': render, setState, forceUpdate, props, and 3 more.
tests/cases/conformance/jsx/file.tsx(29,10): error TS2322: Type 'typeof Button' is not assignable to type 'Button'.


==== tests/cases/conformance/jsx/file.tsx (3 errors) ====
Expand Down Expand Up @@ -40,6 +40,6 @@ tests/cases/conformance/jsx/file.tsx(29,10): error TS2740: Type 'typeof Button'
<Comp a={10} b="hi">
{Button}
~~~~~~
!!! error TS2740: Type 'typeof Button' is missing the following properties from type 'Button': render, setState, forceUpdate, props, and 3 more.
!!! error TS2322: Type 'typeof Button' is not assignable to type 'Button'.
!!! related TS6213 tests/cases/conformance/jsx/file.tsx:29:10: Did you mean to use 'new' with this expression?
</Comp>;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(10,8): error TS2741: Property 'x' is missing in type 'typeof Bar' but required in type 'Bar'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(11,8): error TS2740: Type 'DateConstructor' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(10,8): error TS2322: Type 'typeof Bar' is not assignable to type 'Bar'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(11,8): error TS2322: Type 'DateConstructor' is not assignable to type 'Date'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(17,4): error TS2322: Type '() => number' is not assignable to type 'number'.
tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(26,5): error TS2322: Type '() => number' is not assignable to type 'number'.


Expand All @@ -16,12 +16,11 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(2
foo({
x: Bar,
~~~
!!! error TS2741: Property 'x' is missing in type 'typeof Bar' but required in type 'Bar'.
!!! related TS2728 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:2:5: 'x' is declared here.
!!! error TS2322: Type 'typeof Bar' is not assignable to type 'Bar'.
!!! related TS6213 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:10:8: Did you mean to use 'new' with this expression?
y: Date
~~~~
!!! error TS2740: Type 'DateConstructor' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.
!!! error TS2322: Type 'DateConstructor' is not assignable to type 'Date'.
!!! related TS6213 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:11:8: Did you mean to use 'new' with this expression?
}, getNum());

Expand All @@ -30,7 +29,7 @@ tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts(2
y: new Date()
}, getNum);
~~~~~~
!!! error TS2345: Argument of type '() => number' is not assignable to parameter of type 'number'.
!!! error TS2322: Type '() => number' is not assignable to type 'number'.
!!! related TS6212 tests/cases/compiler/didYouMeanElaborationsForExpressionsWhichCouldBeCalled.ts:17:4: Did you mean to call this expression?


Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts(3,4): error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'.
tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts(3,4): error TS2322: Type 'new () => number' is not assignable to type 'number'.


==== tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts (1 errors) ====
declare var ohno: new () => number;
declare function ff(t: number): void;
ff(ohno)
~~~~
!!! error TS2345: Argument of type 'new () => number' is not assignable to parameter of type 'number'.
!!! error TS2322: Type 'new () => number' is not assignable to type 'number'.
!!! related TS6213 tests/cases/compiler/elaborationForPossiblyCallableTypeStillReferencesArgumentAtTopLevel.ts:3:4: Did you mean to use 'new' with this expression?
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
tests/cases/compiler/functionSignatureAssignmentCompat1.ts(10,21): error TS2322: Type '(delimiter?: string) => ParserFunc' is not assignable to type 'ParserFunc'.
Types of parameters 'delimiter' and 'eventEmitter' are incompatible.
Type 'number' is not assignable to type 'string'.


==== tests/cases/compiler/functionSignatureAssignmentCompat1.ts (1 errors) ====
Expand All @@ -16,7 +14,5 @@ tests/cases/compiler/functionSignatureAssignmentCompat1.ts(10,21): error TS2322:
var d: ParserFunc = parsers.readline; // not ok
~~~~~~~~~~~~~~~~
!!! error TS2322: Type '(delimiter?: string) => ParserFunc' is not assignable to type 'ParserFunc'.
!!! error TS2322: Types of parameters 'delimiter' and 'eventEmitter' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
!!! related TS6212 tests/cases/compiler/functionSignatureAssignmentCompat1.ts:10:21: Did you mean to call this expression?
var e: ParserFunc = parsers.readline(); // ok
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
tests/cases/compiler/optionalParamAssignmentCompat.ts(10,13): error TS2322: Type '(p1?: string) => I1' is not assignable to type 'I1'.
Types of parameters 'p1' and 'p1' are incompatible.
Type 'number' is not assignable to type 'string'.


==== tests/cases/compiler/optionalParamAssignmentCompat.ts (1 errors) ====
Expand All @@ -16,7 +14,5 @@ tests/cases/compiler/optionalParamAssignmentCompat.ts(10,13): error TS2322: Type
var d: I1 = i2.m1; // should error
~~~~~
!!! error TS2322: Type '(p1?: string) => I1' is not assignable to type 'I1'.
!!! error TS2322: Types of parameters 'p1' and 'p1' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
!!! related TS6212 tests/cases/compiler/optionalParamAssignmentCompat.ts:10:13: Did you mean to call this expression?

12 changes: 4 additions & 8 deletions tests/baselines/reference/parser536727.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
Type '(x: string) => string' is not assignable to type 'string'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
Type '(x: string) => string' is not assignable to type 'string'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(7,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5): error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.


==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts (2 errors) ====
Expand All @@ -13,12 +11,10 @@ tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts(8,5):
foo(g);
foo(() => g);
~~~~~~~
!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'.
!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
!!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:7:5: Did you mean to call this expression?
foo(x);
~
!!! error TS2345: Argument of type '() => (x: string) => string' is not assignable to parameter of type '(x: string) => string'.
!!! error TS2345: Type '(x: string) => string' is not assignable to type 'string'.
!!! error TS2322: Type '() => (x: string) => string' is not assignable to type '(x: string) => string'.
!!! related TS6212 tests/cases/conformance/parser/ecmascript5/RegressionTests/parser536727.ts:8:5: Did you mean to call this expression?

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(12,1): error TS2741: Property 'prop' is missing in type 'C' but required in type 'A'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(13,5): error TS2741: Property 'prop' is missing in type 'typeof B' but required in type 'A'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(13,5): error TS2322: Type 'typeof B' is not assignable to type 'A'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(16,5): error TS2741: Property 'prop' is missing in type 'C' but required in type 'B'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(17,5): error TS2741: Property 'prop' is missing in type 'typeof B' but required in type 'B'.
tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts(17,5): error TS2322: Type 'typeof B' is not assignable to type 'B'.


==== tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts (4 errors) ====
Expand All @@ -22,8 +22,7 @@ tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.
!!! related TS2728 tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts:2:5: 'prop' is declared here.
a = B; // error prop is missing
~
!!! error TS2741: Property 'prop' is missing in type 'typeof B' but required in type 'A'.
!!! related TS2728 tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts:2:5: 'prop' is declared here.
!!! error TS2322: Type 'typeof B' is not assignable to type 'A'.
!!! related TS6213 tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts:13:5: Did you mean to use 'new' with this expression?
a = C;

Expand All @@ -33,8 +32,7 @@ tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.
!!! related TS2728 tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts:5:12: 'prop' is declared here.
b = B; // error prop is missing
~
!!! error TS2741: Property 'prop' is missing in type 'typeof B' but required in type 'B'.
!!! related TS2728 tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts:5:12: 'prop' is declared here.
!!! error TS2322: Type 'typeof B' is not assignable to type 'B'.
!!! related TS6213 tests/cases/compiler/staticMemberOfClassAndPublicMemberOfAnotherClassAssignment.ts:17:5: Did you mean to use 'new' with this expression?
b = C;
b = a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerCon
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(24,5): error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(31,5): error TS2411: Property 'c' of type 'number' is not assignable to string index type 'A'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(32,5): error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(37,8): error TS2741: Property 'foo' is missing in type 'typeof A' but required in type 'A'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(38,8): error TS2741: Property 'foo' is missing in type 'typeof B' but required in type 'A'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(37,8): error TS2322: Type 'typeof A' is not assignable to type 'A'.
tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts(38,8): error TS2322: Type 'typeof B' is not assignable to type 'A'.


==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts (8 errors) ====
Expand Down Expand Up @@ -59,12 +59,10 @@ tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerCon
var b: { [x: string]: A } = {
a: A,
~
!!! error TS2741: Property 'foo' is missing in type 'typeof A' but required in type 'A'.
!!! related TS2728 tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts:4:5: 'foo' is declared here.
!!! error TS2322: Type 'typeof A' is not assignable to type 'A'.
!!! related TS6213 tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts:37:8: Did you mean to use 'new' with this expression?
b: B
~
!!! error TS2741: Property 'foo' is missing in type 'typeof B' but required in type 'A'.
!!! related TS2728 tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts:4:5: 'foo' is declared here.
!!! error TS2322: Type 'typeof B' is not assignable to type 'A'.
!!! related TS6213 tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts:38:8: Did you mean to use 'new' with this expression?
}
5 changes: 2 additions & 3 deletions tests/baselines/reference/typeMatch1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/typeMatch1.ts(18,1): error TS2322: Type 'D' is not assignable to type 'C'.
Types have separate declarations of a private property 'x'.
tests/cases/compiler/typeMatch1.ts(19,4): error TS2741: Property 'x' is missing in type 'typeof C' but required in type 'C'.
tests/cases/compiler/typeMatch1.ts(19,4): error TS2322: Type 'typeof C' is not assignable to type 'C'.
tests/cases/compiler/typeMatch1.ts(20,1): error TS2367: This condition will always return 'false' since the types 'typeof C' and 'typeof D' have no overlap.


Expand Down Expand Up @@ -28,8 +28,7 @@ tests/cases/compiler/typeMatch1.ts(20,1): error TS2367: This condition will alwa
!!! error TS2322: Types have separate declarations of a private property 'x'.
x6=C;
~
!!! error TS2741: Property 'x' is missing in type 'typeof C' but required in type 'C'.
!!! related TS2728 tests/cases/compiler/typeMatch1.ts:12:19: 'x' is declared here.
!!! error TS2322: Type 'typeof C' is not assignable to type 'C'.
!!! related TS6213 tests/cases/compiler/typeMatch1.ts:19:4: Did you mean to use 'new' with this expression?
C==D;
~~~~
Expand Down
Loading