Skip to content

Make finer-grained errors get reported on function arguments #26784

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
Aug 30, 2018
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
12 changes: 8 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10527,9 +10527,13 @@ namespace ts {
* attempt to issue more specific errors on, for example, specific object literal properties or tuple members.
*/
function checkTypeAssignableToAndOptionallyElaborate(source: Type, target: Type, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
if (isTypeAssignableTo(source, target)) return true;
if (!elaborateError(expr, source, target)) {
return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain);
return checkTypeRelatedToAndOptionallyElaborate(source, target, assignableRelation, errorNode, expr, headMessage, containingMessageChain);
}

function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
if (isTypeRelatedTo(source, target, relation)) return true;
if (!errorNode || !elaborateError(expr, source, target)) {
return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain);
}
return false;
}
Expand Down Expand Up @@ -18868,7 +18872,7 @@ namespace ts {
// we obtain the regular type of any object literal arguments because we may not have inferred complete
// parameter types yet and therefore excess property checks may yield false positives (see #17041).
const checkArgType = excludeArgument ? getRegularTypeOfObjectLiteral(argType) : argType;
if (!checkTypeRelatedTo(checkArgType, paramType, relation, reportErrors ? arg : undefined, headMessage)) {
if (!checkTypeRelatedToAndOptionallyElaborate(checkArgType, paramType, relation, reportErrors ? arg : undefined, arg, headMessage)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(1,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(4,5): error TS2345: Argument of type '{ id: number; name: boolean; }' is not assignable to parameter of type '{ id: number; name?: string; }'.
Types of property 'name' are incompatible.
Type 'boolean' is not assignable to type 'string'.
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(4,17): error TS2322: Type 'false' is not assignable to type 'string'.
tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(5,5): error TS2345: Argument of type '{ name: string; }' is not assignable to parameter of type '{ id: number; name?: string; }'.
Property 'id' is missing in type '{ name: string; }'.

Expand All @@ -13,10 +11,9 @@ tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts(5,5): error TS
foo({ id: 1234 }); // Ok
foo({ id: 1234, name: "hello" }); // Ok
foo({ id: 1234, name: false }); // Error, name of wrong type
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ id: number; name: boolean; }' is not assignable to parameter of type '{ id: number; name?: string; }'.
!!! error TS2345: Types of property 'name' are incompatible.
!!! error TS2345: Type 'boolean' is not assignable to type 'string'.
~~~~
!!! error TS2322: Type 'false' is not assignable to type 'string'.
!!! related TS6500 tests/cases/compiler/assignmentCompatFunctionsWithOptionalArgs.ts:1:31: The expected type comes from property 'name' which is declared here on type '{ id: number; name?: string; }'
foo({ name: "hello" }); // Error, id required but missing
~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ name: string; }' is not assignable to parameter of type '{ id: number; name?: string; }'.
Expand Down
14 changes: 5 additions & 9 deletions tests/baselines/reference/declarationsAndAssignments.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,11):
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(73,14): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,11): error TS2459: Type 'undefined[]' has no property 'a' and no string index signature.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(74,14): error TS2459: Type 'undefined[]' has no property 'b' and no string index signature.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(106,5): error TS2345: Argument of type '[number, [string, { y: false; }]]' is not assignable to parameter of type '[number, [string, { x: any; y?: boolean; }]]'.
Type '[string, { y: false; }]' is not assignable to type '[string, { x: any; y?: boolean; }]'.
Type '{ y: false; }' is not assignable to type '{ x: any; y?: boolean; }'.
Property 'x' is missing in type '{ y: false; }'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(106,17): error TS2322: Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
Property 'x' is missing in type '{ y: boolean; }'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,6): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assignable to type 'string'.

Expand Down Expand Up @@ -170,11 +168,9 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
f14([2, ["abc", { x: 0, y: true }]]);
f14([2, ["abc", { x: 0 }]]);
f14([2, ["abc", { y: false }]]); // Error, no x
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[number, [string, { y: false; }]]' is not assignable to parameter of type '[number, [string, { x: any; y?: boolean; }]]'.
!!! error TS2345: Type '[string, { y: false; }]' is not assignable to type '[string, { x: any; y?: boolean; }]'.
!!! error TS2345: Type '{ y: false; }' is not assignable to type '{ x: any; y?: boolean; }'.
!!! error TS2345: Property 'x' is missing in type '{ y: false; }'.
~~~~~~~~~~~~
!!! error TS2322: Type '{ y: boolean; }' is not assignable to type '{ x: any; y?: boolean; }'.
!!! error TS2322: Property 'x' is missing in type '{ y: boolean; }'.

module M {
export var [a, b] = [1, 2];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,4): error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,8): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,29): error TS1005: ',' expected.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(8,4): error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'.
Types of property 'length' are incompatible.
Type '4' is not assignable to type '3'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(16,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(16,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(23,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'.
Types of property 'x' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(23,16): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(30,14): error TS2300: Duplicate identifier 'z'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(30,18): error TS2300: Duplicate identifier 'z'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(34,4): error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'.
Types of property 'z' are incompatible.
Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(34,6): error TS2322: Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(35,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'.
Property 'z' is missing in type '{}'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(36,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'.
Types of property 'z' are incompatible.
Type 'boolean' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(37,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'.
Types of property 'z' are incompatible.
Type 'boolean' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(38,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'.
Types of property 'b' are incompatible.
Type 'boolean' is not assignable to type 'string | number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(39,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'.
Types of property '2' are incompatible.
Type 'boolean' is not assignable to type '[[any]]'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(40,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number?]]]'.
Type '[[string]]' is not assignable to type '[[number?]]'.
Type '[string]' is not assignable to type '[number?]'.
Types of property '0' are incompatible.
Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(36,6): error TS2322: Type 'true' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(37,6): error TS2322: Type 'false' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(38,6): error TS2322: Type 'true' is not assignable to type 'string | number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(39,11): error TS2322: Type 'false' is not assignable to type '[[any]]'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(40,13): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(46,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(47,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(56,8): error TS2463: A binding pattern parameter cannot be optional in an implementation signature.
Expand All @@ -54,9 +37,8 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
// If the declaration includes a type annotation, the parameter is of that type
function a0([a, b, [[c]]]: [number, number, string[][]]) { }
a0([1, "string", [["world"]]); // Error
~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
~
!!! error TS1005: ',' expected.
a0([1, 2, [["world"]], "string"]); // Error
Expand All @@ -83,10 +65,9 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
function b3([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { }

b1("string", { x: "string", y: true }); // Error
~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'.
!!! error TS2345: Types of property 'x' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
!!! related TS6500 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts:19:29: The expected type comes from property 'x' which is declared here on type '{ x: number; y: any; }'

// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3)
function c0({z: {x, y: {j}}}) { }
Expand All @@ -102,41 +83,29 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(
function c6([a, b, [[c = 1]]]) { }

c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} }
~~~~~~~~
!!! error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'.
!!! error TS2345: Types of property 'z' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'.
~
!!! error TS2322: Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'.
c1({}); // Error, implied type is {z:number}?
~~
!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'.
!!! error TS2345: Property 'z' is missing in type '{}'.
c1({ z: true }); // Error, implied type is {z:number}?
~~~~~~~~~~~
!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'.
!!! error TS2345: Types of property 'z' are incompatible.
!!! error TS2345: Type 'boolean' is not assignable to type 'number'.
~
!!! error TS2322: Type 'true' is not assignable to type 'number'.
!!! related TS6500 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts:27:21: The expected type comes from property 'z' which is declared here on type '{ z: number; }'
c2({ z: false }); // Error, implied type is {z?: number}
~~~~~~~~~~~~
!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'.
!!! error TS2345: Types of property 'z' are incompatible.
!!! error TS2345: Type 'boolean' is not assignable to type 'number'.
~
!!! error TS2322: Type 'false' is not assignable to type 'number'.
c3({ b: true }); // Error, implied type is { b: number|string }.
~~~~~~~~~~~
!!! error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'.
!!! error TS2345: Types of property 'b' are incompatible.
!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'.
~
!!! error TS2322: Type 'true' is not assignable to type 'string | number'.
!!! related TS6500 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts:29:20: The expected type comes from property 'b' which is declared here on type '{ b: string | number; }'
c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]]
~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'.
!!! error TS2345: Types of property '2' are incompatible.
!!! error TS2345: Type 'boolean' is not assignable to type '[[any]]'.
~~~~~
!!! error TS2322: Type 'false' is not assignable to type '[[any]]'.
c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer
~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number?]]]'.
!!! error TS2345: Type '[[string]]' is not assignable to type '[[number?]]'.
!!! error TS2345: Type '[string]' is not assignable to type '[number?]'.
!!! error TS2345: Types of property '0' are incompatible.
!!! error TS2345: Type 'string' is not assignable to type 'number'.
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

// A parameter can be marked optional by following its name or binding pattern with a question mark (?)
// or by including an initializer. Initializers (including binding property or element initializers) are
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts(26,4): error TS2345: Argument of type '[number, number, [[string]], boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'.
Types of property 'length' are incompatible.
Type '5' is not assignable to type '3'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts(29,5): error TS2345: Argument of type '[number, number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]], ...any[]]'.
Types of property '2' are incompatible.
Type 'number' is not assignable to type '[[any]]'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts(29,12): error TS2322: Type 'number' is not assignable to type '[[any]]'.
tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts(30,5): error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]], ...any[]]'.
Property '2' is missing in type '[number, number]'.

Expand Down Expand Up @@ -42,10 +40,8 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.

a10([1, 2, [["string"]], false, true]); // Parameter type is any[]
a10([1, 2, 3, false, true]); // Parameter type is any[]
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type '[number, number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]], ...any[]]'.
!!! error TS2345: Types of property '2' are incompatible.
!!! error TS2345: Type 'number' is not assignable to type '[[any]]'.
~
!!! error TS2322: Type 'number' is not assignable to type '[[any]]'.
a10([1, 2]); // Parameter type is any[]
~~~~~~
!!! error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]], ...any[]]'.
Expand Down
Loading