【问题标题】:How to check arguments passed to a function in Typescript? [duplicate]如何检查传递给 Typescript 中函数的参数? [复制]
【发布时间】:2019-12-18 09:44:24
【问题描述】:

我正在使用 Typescript 编写代码,然后将我的 TS 代码编译成 JS。 我被困在这里,因为我不知道如何解决下面提到的错误:

第 2 行的 TypeScript 错误 - 错误 TS2304:找不到名称“参数”

我的 TS 代码:

1        function testSomething() {
2            var paramsPassed = arguments;
3            console.log("Params are : ", paramsPassed);
4            return arguments.length;
5        }
6
7        const result = testSomething("this", "is", "a", "demo");
8        console.log("Output I want: " , result);

我不知道从哪里导入这个或如何完成我的任务。

【问题讨论】:

  • arguments 应该是什么?
  • arguments 在这种情况下,返回传递给函数的 所有参数的数组。您可以在浏览器控制台中运行我的代码块...

标签: javascript arrays typescript function parameters


【解决方案1】:

arguments 关键字仅在 JavaScript 文件中有效,在 TypeScript 文件中无效。

如下改变函数,

function testSomething(... arguments: string[])

请查看下面的参考链接, https://stackoverflow.com/a/12697349/11914056

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多