使用 Spread 运算符时出现 TypeScript 错误?

标签 typescript visual-studio-code

enter image description here 每当我使用如下所示的传播运算符时

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

我在编辑器中遇到这个错误

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

为什么在使用扩展运算符传递参数时 TypeScript 会报错?

当我实际运行代码时没有错误,传播运算符只是让我将数组用作函数的参数,但在 VSCode 中它向我显示错误,就好像我不能。

最佳答案

类型展开运算符仅在所有参数都标记为可选时才有效

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

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

关于使用 Spread 运算符时出现 TypeScript 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45225128/

相关文章:

javascript - 无法使我的 Highcharts 以 Angular 5 响应

javascript - 如何使用 Typescript 模块

typescript - tsc 编译使用文件扩展名导入的 typescript

visual-studio-code - 如何在不从键盘移动 Visual Studio Code 中的光标的情况下滚动

java - 如何解决 java.lang.NumberFormatException : For input string: "TooLow"

visual-studio-code - 如何在VS Code中关闭 “matching”高亮显示?

javascript - typeScript init 动态类按名称调用 (2345)

Typescript、index.d.ts 和 webpack : Module not found error

debugging - 延迟启动VS Code中的化合物调试

typescript - Vue/TypeScript/ESLint/Prettier/Vetur 格式不起作用