打字稿无法使用target = es6

时间:2020-05-22 14:30:09

标签: javascript typescript es6-class ts-node

尽管类属性是在构造函数中声明和初始化的,但无法识别。这是在将node.js和ts-node与typescript一起使用的情况下。仅当tsconfig target = es6(或es2015)时才会发生这种情况;如果是es5,则可以正常运行。碰巧这是一个父类,但是错误消息指出该类本身缺少属性。

类定义:

class Page {
private _map: any
get map() {
    return this._map
}
set map(value) {
    this._map = value
}
public element = new Element()
public spinner ;
constructor() {
    this.spinner = legacyBrowser._spinner
    this.element = new Element()
}

错误是

base.page.ts(13,14): error TS2339: Property 'spinner' does not   exist on type 'Page'.
base.page.ts(14,14): error TS2339: Property 'element' does not exist on type 'Page'. common/pageObjects
base.page.ts(17,21): error TS2551: Property '_map' does not exist on type 'Page'. Did you mean 'map'?

tsconfig是

"compilerOptions": {
  "target": "es6",
  "baseUrl": ".",
  "outDir": "out",
  "paths": {
    "*": [
      "./*"
    ],
    "src/*": [
      "./src/*"
    ]
  },
  "types": [
    "node",
    "@wdio/sync",
    "@wdio/cucumber-framework",
    "cucumber"
  ],
  "allowJs": true,
  "checkJS": false;
  "esModuleInterop": true,
  "strict": false,
  "downlevelIteration": true,
  "typeRoots": [
    "./declarations",
    "./typeDefs"
  ],
  "allowSyntheticDefaultImports": true

}
}

此处Typescript TS2339 Property doesn't exist on type - Class decleration报告了相同的问题,但在这种情况下,答案是webpack引起了问题,但此处未使用Webpack。

0 个答案:

没有答案