Typescript: Expected 1-3 arguments, but got 2

Created on 16 Oct 2017  路  14Comments  路  Source: microsoft/TypeScript

TypeScript Version: 2.6.0-dev.20171015

Code

declare function f(x: number): number;
declare function f(x: number, y: number, z: number): number;

f(1, 2);

Expected behavior:

Expected 1 or 3 arguments, but got 2.

Actual behavior:

src/a.ts(4,1): error TS2554: Expected 1-3 arguments, but got 2.

Bug Error Messages Fixed good first issue help wanted

Most helpful comment

Consider something like No overload expects {0} arguments. The most likely overloads that match expect either {1} arguments or at least {2} arguments.

All 14 comments

But what if it was:

declare function f(x: number): number;
declare function f(x: number, y: number, z: number): number;
declare function f(x: number, y: number, z: number, a: number): number;
declare function f(x: number, y: number, z: number, a: number, b: number): number;
...
declare function f(x: number, y: number, z: number, a: number, b: number, ...): number;

f(1, 2);

The message would be Expected 1 or 3 or 4 or 5 or ... or N arguments, but got 2. ???

Based on existing error messages, it would be ideal to say Expected 1 or at least 3 arguments, but got 2..
Of course, doing this in a localizable way without defining too many different diagnostics will be more difficult; might be better to use commas instead of or (except for the last one) so that the template would be Expected {0} or at least {1} arguments and fill in {0} with a comma-separated list if necessary.

Consider something like No overload expects {0} arguments. The most likely overloads that match expect either {1} arguments or at least {2} arguments.

@DanielRosenwasser Good, assuming that "most likely" would mean the next lowest and next highest numbers of arguments. So if overloads accepted 1, 2, or 5 arguments and you provide 3 or 4, say "2 or 5".

Yup, that's what I should've been explicit about, thanks for clarifying. 馃槂

Note that this has occurred at least once in real code: DefinitelyTyped/DefinitelyTyped#19119.

this also happened on https://www.npmjs.com/package/@types/urijs library when we are calling addQuery() with 2 arguments

import * as URI from 'urijs';

const path = new URI(`/some-url`);
path.addQuery('pickUpCoordinate', 'foo-query'); // this will throw TS2554 expected 1 argument but got 2 arguments

That's because addQuery only expects 1 argument:

    interface URI {
        absoluteTo(path: string): URI;
        absoluteTo(path: URI): URI;
        addFragment(fragment: string): URI;
        addQuery(qry: string): URI;
        addQuery(qry: Object): URI;
        // ......

Maybe @types/urijs needs to be updated.

Very similar issue:

// lib ArrayConstructor sig for reference
interface ArrayConstructor {
    from<T>(iterable: Iterable<T> | ArrayLike<T>): T[];
    from<T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[];
}

// [ts] Expected 1-3 arguments, but got 1.
Array.from<number, number>([1, 2, 3])

I ran into this too: https://goo.gl/ufcpDP.

Error TS2554, expected argument 2 but got 1.
How I can solve it?
sign(myForm){
if(myForm.valid){
this.af.auth.createUserWithEmailAndPassword({
email:this.email,
password:this.password
}).then(
(success)=> {
console.log(success);
this.router.navigate(['/login'])
}).catch(
(err)=> {
console.log(err);
this.error=err;
})

@durjoy That probably depends on the library you're using. Look for a help forum for that -- it's probably not a bug in the compiler.

this only happens when you build the project.
for serve, project run smoothly.
there should be reason behind this at all for giving number of argument in a function
ERROR in src\app\component\crm\smslist\list\list.component.html(11,19): : Expected 1 arguments, but got 2.

This isn't a support forum and if you think you've found a bug you should file that separately.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

seanzer picture seanzer  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

dlaberge picture dlaberge  路  3Comments

wmaurer picture wmaurer  路  3Comments