WebStorm无法识别Ng2的“只读”?

时间:2016-05-23 02:27:39

标签: angularjs typescript webstorm

WebStorm编译错误。以下代码来自ng2:

export declare class NgForRow {
    ....
    constructor($implicit: any, index: number, count: number);
    //when remove readonly,it works!
    readonly first: boolean;//has compile error
    readonly last: boolean;//has compile error
    readonly even: boolean;//has compile error
    readonly odd: boolean;//has compile error
}
  

错误:(7,14)TS1005:'='预期。

     

错误:(7,19)TS1005:';'预期

     

错误:(8,14)TS1005:'='预期。

     

错误:(8,18)TS1005:';'预期

enter image description here

3 个答案:

答案 0 :(得分:3)

与WebStorm捆绑在一起的TypeScript编译器(在最新版本中为1.8.10)并没有“只读”'修饰符支持。您可以尝试使用独立的TypeScript编译器,方法是选择' custom'编译器版本 - 请参阅https://www.jetbrains.com/help/webstorm/2016.1/transpiling-typescript-to-javascript.html#d239602e138

请参阅WEB-21317

答案 1 :(得分:1)

Webstorm尚不支持TypeScript 2.0。

更多:https://basarat.gitbooks.io/typescript/content/docs/types/readonly.html

选项

使用支持TypeScript前沿的其他编辑器(我正在编写alm.tools并编写atom-typescript)或只是容忍IDE中的错误(但在控制台上保持绿色)。

答案 2 :(得分:0)

最简单的方法是不编译依赖项,您只需要编译应用程序。在tsconfig.json文件中,请务必排除node_modules:

  //tsconfig.json
  {
     "compilerOptions": {
      "target": "es5",
       ...
     },
     "exclude": [
        "node_modules",
        "typings/main",
        "typings/main.d.ts"
     ]
  }

确保将webstorm设置为使用tsconfig。您可能必须重新启动WS,因为我必须这样做才能让它实际加载更改。这应该绕过并需要升级到TS2,直到它准备好生产。

诀窍是什么?

您可能认为忽略node_modules目录将不允许TS加载定义,但不是这样。在Angular2类中,您明确导入模块:

import { OnInit } from '@angular/core';

因此,只要准确配置了模块加载器,就会显式加载这些模块的定义:

"module": "commonjs",
"moduleResolution": "node",
相关问题