3

TypeScript doesn't show "Expected 1 arguments, but got 0." error, when it should.

type PolyElement = [number, number];

export interface Node {
  element: PolyElement | null;
  next: Node | null;
}
export class Node {
  constructor(element?: PolyElement) {
    this.element = element ?? null;
    this.next = null;
  }
  add(...args: PolyElement[]) {
    for (let i = 0; i < args.length; ++i) {
       //some code
      }
    }
  }

then I invoke method like so:

poly1.add([1,2,3]);

and it outputs: Argument of type '[number, number, number]' is not assignable to parameter of type 'PolyElement'. Types of property 'length' are incompatible. Type '3' is not assignable to type '2'.ts(2345)

also

poly1.add([]);

outputs: Argument of type '[]' is not assignable to parameter of type 'PolyElement'. Type '[]' is missing the following properties from type '[number, number]': 0, 1ts(2345)

etc.

The QUESTION is why:

poly1.add();

DOESN'T output something like: Expected at least 1 arguments, but got 0. ?

5
  • 1
    Your answer is already in your question: Argument of type '[]'.... Empty array counts as an argument, it is just of the wrong type. Commented Jul 17, 2020 at 21:29
  • sorry, I made a mistake when copying. I already corrected my question. Now it is ` poly1.add();` Commented Jul 17, 2020 at 21:32
  • Does this answer your question? Expect at least one argument for variadic method in TypeScript Commented Jul 17, 2020 at 21:51
  • 1
    @EmileBergeron yes, but there is a better reply to this question in this thread that in the other you linked Commented Jul 17, 2020 at 22:06
  • Yep, now there is! Ideally, the other questions should be linked to this newer one. Commented Jul 17, 2020 at 22:15

2 Answers 2

5

This syntax

add(...args: PolyElement[]) {

means "Pass me as many arguments (including zero) as you like".

You can write this instead to enforce at least one argument:

add(...args: [PolyElement, ...PolyElement[]]) {
Sign up to request clarification or add additional context in comments.

6 Comments

could you help what is the best syntax to pass more than 0 arguments ot type PolyElement?
Maybe add(el: PolyElement, ...args: PolyElement[])? But you'd need to handle el explicitly.
In all honesty you got this before me. I just added the bit to enforce at least 1 PolyElement.
That's the thing, you need to use it, if not, it means that you're explicitly ignoring the first parameter.
Now I prefer this answer (still upvoted both). And while looking for a duplicate candidate, didn't see anything similar.
|
3

Rest arguments ... means 0 or more. If you want at least 1, then you should explicitly accept a first argument and then accept "the rest".

type NumArr = [number, number];

// Forces at least 1 NumArr
const foo = (a: NumArr, ...b: NumArr[]) => {
    const all = [a, ...b];
    console.log(all);
}

const bar = () => {
    foo([1, 2]); // Good!
    foo([]); // Wrong type, [] is not [number, number]
    foo(); // Expected at least 1 arg
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.