Webstorm 中的 TypeScript : string array indexOf Object error

标签 typescript

这里有两个变量:

var stringA : string = "a";
var stringAr : string[] = [ "a", "b", "c" ];

如果我尝试执行以下操作,则会收到错误消息:

stringAr.indexOf(stringA);

出现以下错误(“stringA”下的红线):

参数类型字符串不能分配给参数对象

有人知道我为什么会收到这个吗?我目前使用的是Webstorm 6.0。

最佳答案

这在 Visual Studio 和 playground 中按预期工作。 :

var stringA : string = "a";
var stringAr : string[] = [ "a", "b", "c" ];

var idx:number = stringAr.indexOf(stringA);

console.log("Index: " + idx);  // Logs "Index: 0"

你有latest version of TS吗? (撰写本文时为 0.8.3)并且您确定在某处没有大写的 String 而不是 string (如所述 here )?

关于Webstorm 中的 TypeScript : string array indexOf Object error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15400665/

相关文章:

typescript - `///<reference types="react-scripts"/>` 是什么意思?我们可以在 *.d.ts 中使用什么其他类似 xml 的语法?

javascript - 如何在 Angular 2中设置或删除当前路由中的queryParams而不重新加载页面

javascript - Node.js 事件发射器 : How to bind a class context to the event listener and then remove this listener

javascript - 为什么我不必将 `import` `anti:fake` 放入我的 Meteor 源文件中?

angular - 如何打开和关闭输入复选框(更改)事件值?

javascript - Node JS 同步读取一行文本

typescript - 在 Typescript Vue2 组件中输入 this.$store

javascript - 如何使用 typescript 通过切换按钮显示 html 表的内联详细信息

typescript - NestJS GraphQL 数据源

javascript - typescript /javascript 方法应该计算并返回 css 类吗?