Skip to content
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

Expected at least x arguments, but got x or more #28010

Closed
robbiespeed opened this issue Oct 20, 2018 · 7 comments · Fixed by #43855
Closed

Expected at least x arguments, but got x or more #28010

robbiespeed opened this issue Oct 20, 2018 · 7 comments · Fixed by #43855
Labels
Domain: Related Error Spans Specifying regions for error messages/diagnostics on multiple locations. Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript
Milestone

Comments

@robbiespeed
Copy link

robbiespeed commented Oct 20, 2018

TypeScript Version: 3.2.0-dev.20181019 and 3.1.3

Search Terms: function arguments count, spread, rest
Code

function foo(a: number, ...r: number[]): void { };

const a = [1];

foo(...a, 2); // Expected at least 1 arguments, but got 1 or more.
foo(...a, 2, 3); // Expected at least 1 arguments, but got 2 or more.

Expected behavior:
No errors
"at least 1" == "1 or more"

Actual behavior:
"at least 1" != "1 or more"
Non logical type error.

Playground Link: here

Related Issues:
#26350

@fatcerberus
Copy link

Well that’s kind of a nasty bug. Seems like the spread operator is tripping it up somehow. Possibly a bad interaction between using a spread at the call site and a rest parameter in the declaration?

@robbiespeed
Copy link
Author

Definitely some bugs with interpreting spread, even without using rest in the function:

function bar(a: number, b: number): void { };
function baz(a: number): void { };

const a: number[] = [];

bar(...a, 2); // Correctly interprets getting "1 or more"" arguments 
bar(...a, 2, 3); // Incorrectly interprets getting "3 or more"" arguments > Should be "2 or more""

baz(...a, 2); // Incorrectly interprets getting "2 or more"" arguments > Should be "1 or more""
baz(...a, 2, 3); // Incorrectly interprets getting "3 or more"" arguments > Should be "2 or more""

Playground

Though these don't have a critical impact since they are correct errors, just incorrect error messages, unlike the top bug. Perhaps related though.

@fatcerberus
Copy link

Based on #26350 it seems that the underlying issue is that the compiler balks when a spread potentially crosses a formal argument boundary (which will always be the case if it’s not in tail position and the thing being spreaded is anything other than a 1-tuple).

@weswigham weswigham added Bug A bug in TypeScript Domain: Related Error Spans Specifying regions for error messages/diagnostics on multiple locations. Experience Enhancement Noncontroversial enhancements labels Oct 23, 2018
@weswigham
Copy link
Member

So there's two things here: clarifying the error messages for most cases to explain the error better, and possibly improving how spread handles arrays? The last one seems less likely, but the error message improvements should be uncontentious.

@andrewbranch
Copy link
Member

It would be possible to “fix” this case (i.e., not issue errors) only because the type of the fixed parameter (number) is the same as the element type of the rest parameters. If it were foo(a: number, ...r: string[]), we currently issue the same error message, but the real issue is not about how many arguments might be passed depending on the length of the spread array, it’s about how the types of arguments will line up with the parameter types depending on the length of the spread array. That problem will always require an error message, and the special case where all the parameters have the same type seems suspiciously unuseful. We suspect that most of the upvotes here come either from an earlier bug where even spreading tuples of known length into parameters was not allowed (if a is initialized to [1] as const, there is no error—but there used to be one), or from the fact that the error message itself is nonsense.

A better error would be placed on the spread element itself, and convey the general idea of “expected a fixed argument, but got a variable-length spread.”

@evanjmg
Copy link

evanjmg commented Oct 21, 2020

best workaround solution is to put a dummy argument unfortunately or retype the input ie. lodash merge
merge({}, [...])
though this still is a bug.

danielroe added a commit to nuxt-community/composition-api that referenced this issue Nov 9, 2020
@YanYuanFE
Copy link

YanYuanFE commented Mar 23, 2021

import { pipe } from "rxjs";

validates.length > 0 ? pipe(...validates) : undefined

image
"typescript": "^4.2.3",

Tohman21 pushed a commit to Tohman21/js-handy-filter that referenced this issue Mar 25, 2021
Fix bugs in ConditionParser;
Tests for getSimpleConditionClassByKey;
Fix imports;
Aliases for Greater and GreaterOrEqual condition;
Fix the TypeScript bug (microsoft/TypeScript#28010).
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Apr 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Related Error Spans Specifying regions for error messages/diagnostics on multiple locations. Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants