【问题标题】:Typescript, NodeJS, ES6 - findIndex on arrayTypescript、NodeJS、ES6 - 数组上的 findIndex
【发布时间】:2016-04-19 01:23:58
【问题描述】:

所以我在 typescript 和 NodeJS 的数组上使用 findIndex ES6 函数时遇到了问题。

我得到的错误信息是:

requestType.findIndex 不是函数

这是我的代码:

let weatherStates:Array<string> = ['current', 'forecast'];
let requestType:string = 'current';
requestType.findIndex(weatherStates);

然后错误被抛出,不过我可以使用各种其他 ES6 功能,比如箭头函数等。

看起来 polyfill 在 lib.core.d.ts 类型中可用。

这里是 mytsconfig.json 设置:

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "sourceMap": true
},
"exclude": [
    "node_modules",
    "typings"
]
}

我是相当新的打字稿,所以任何帮助将不胜感激!

【问题讨论】:

  • 1. requestType 是字符串,而不是数组 2。Array.prototype.index 接受回调。也许你想要weatherStates.indexOf(requestType)
  • 谢谢,帮了大忙!

标签: javascript node.js typescript ecmascript-6


【解决方案1】:

requestType 在您的示例中不是数组。它是一个字符串。

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 2021-12-27
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多