Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

enter image description here Whenever I use the spread operator such as below

public drawTextTest(p1: number, p2: number, p3: number):void {
    console.log(p1, p2, p3);
}
let array = [2, 2, 5];
this.drawTextTest( ... array );

I get this error in the editor

[ts] Expected # arguments, but got a minimum of 0.

Why does TypeScript give an error when using the spread operator to pass arguments?

There's no error when I actually run the code, the spread operator is simply letting me use the array as arguments to a function yet in VSCode it shows me the error as if I couldn't.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
923 views
Welcome To Ask or Share your Answers For Others

1 Answer

typed spread operator works only when all parameters are marked as optional

public drawTextTest(p1?: number, p2?: number, p3?: number):void {

see https://github.com/Microsoft/TypeScript/issues/4130#issuecomment-303486552


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...