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

Categories

0 votes
955 views
in Technique[技术] by (71.8m points)

visual studio code - TypeScript error when using the Spread operator?

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...