TypeScript error when using the Spread operator?

后端 未结 3 1437
眼角桃花
眼角桃花 2020-12-11 00:35

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 = [         


        
3条回答
  •  时光说笑
    2020-12-11 00:45

    Vscode may be using a version of typescript before 2.1 to evaluate the code. Double-check the version in the bottom right of your IDE window.

    If I'm wrong about that, I'll need to see your drawInfo object definition. My second guess is that drawInfo has optional properties and the evaluator is seeing the possibility that the spread would result in 0 params.

    EDIT: drawInfo appears to be an array. Because the length of arrays can be changed and nothing guarantees that there are 3 properties, the ts evaluator will complain.

    If the drawInfo will always contain 3 values, you may want to change it from an array to a defined type and avoid the spread operator.

提交回复
热议问题