“不能在环境上下文中声明访问器。”在自定义库中

时间:2019-12-01 11:51:44

标签: angular typescript

我有一个webpack,打字稿项目(A),它发出一个小的库。该库用在一个角度为8的项目(B)中。

在项目A中,我声明了一个像这样的类:

export class Foo {

    private foo: string;

    constructor(foo: string) {
        this.foo = foo;
    }

}

export class Bar extends Foo {

    private bla: string;

    constructor(foo: string, bla: string) {
        super(foo);
        this.bla = bla;
    }

    get Bla(): string {
        return this.bla;
    }

}

这样就可以了,但是无论我采用哪种Bla访问器的打字稿版本,在相应的get Bla(): string文件中都会发出.d.ts

当我在有角度的项目中使用该软件包时,我得到:

ERROR in ../node_modules/.../lib/types/src/.../.../xyz.d.ts:6:9 - error TS1086: An accessor cannot be declared in an ambient context.

6     get Bla(): string;

根据this answer,它应该有助于将打字稿更新为3.7,我尝试过不起作用。

我知道为访问器创建的类型由于某种原因未正确创建。请帮助我了解为什么以及什么可能的解决方案。

这是我对项目A的tsconfig.json

{
  "compilerOptions": {
    "outDir": "lib/types",
    "declaration": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "noUnusedLocals": false
  },
  "exclude": [
    "node_modules",
    "lib",
    "tests"
  ]
}

项目A的依赖项:

  "devDependencies": {
    "@types/chai": "^4.2.5",
    "@types/gl-matrix": "^2.4.5",
    "@types/jsdom": "^12.2.4",
    "@types/mocha": "^5.2.7",
    "@types/node": "^12.12.8",
    "@types/webgl2": "0.0.5",
    "@types/webpack-dev-middleware": "^2.0.3",
    "chai": "^4.2.0",
    "clean-webpack-plugin": "^3.0.0",
    "compression-webpack-plugin": "^3.0.0",
    "file-loader": "^4.2.0",
    "html-webpack-plugin": "^3.2.0",
    "image-webpack-loader": "^6.0.0",
    "jsdom": "^15.2.1",
    "mocha": "^6.2.2",
    "nyc": "^14.1.1",
    "style-loader": "^1.0.0",
    "terser-webpack-plugin": "^2.2.1",
    "ts-loader": "^6.2.1",
    "ts-node": "^8.5.2",
    "tslint": "^5.20.1",
    "tslint-loader": "^3.5.4",
    "typescript": "^3.7.2",
    "webpack": "^4.41.2",
    "webpack-bundle-analyzer": "^3.6.0",
    "webpack-cli": "^3.3.10",
    "webpack-dev-middleware": "^3.7.2",
    "webpack-dev-server": "^3.9.0",
    "webpack-glsl-loader": "^1.0.1",
    "webpack-merge": "^4.2.2"
  }

tsconfig.json对于项目B:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "downlevelIteration": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

项目B的依赖项:

  "dependencies": {
    "@angular/animations": "^8.2.14",
    "@angular/cdk": "~7.3.7",
    "@angular/common": "^8.2.14",
    "@angular/compiler": "^8.2.14",
    "@angular/core": "^8.2.14",
    "@angular/forms": "^8.2.14",
    "@angular/material": "^7.3.7",
    "@angular/platform-browser": "^8.2.14",
    "@angular/platform-browser-dynamic": "^8.2.14",
    "@angular/router": "^8.2.14",
    "@types/gl-matrix": "^2.4.5",
    "@types/stats": "^0.16.30",
    "core-js": "^2.6.10",
    "file-saver": "^2.0.2",
    "gl-matrix": "^3.1.0",
    "hammerjs": "^2.0.8",
    "material-community-components": "^3.4.3",
    "rxjs": "~6.5.3",
    "stats-js": "^1.0.1",
    "stats.js": "^0.17.0",
    "tslib": "^1.9.0",
    "ukrnall-gltf-loader": "file://../urknall-gltf-loader/pkg/",
    "urknall": "file:../urknall/urknall-0.0.1.tgz",
    "zone.js": "~0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.19",
    "@angular/cli": "^8.3.20",
    "@angular/compiler-cli": "^8.2.14",
    "@angular/language-service": "^8.2.14",
    "@types/file-saver": "^2.0.1",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^8.10.59",
    "@types/webgl2": "0.0.4",
    "codelyzer": "^5.2.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.1.0",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "^3.5.3"
  }

如有必要,我还可以提供项目A的webpack配置。

2 个答案:

答案 0 :(得分:1)

您的问题似乎与链接的question相反:

您实际上需要确保项目A是使用typescript版本<3.7编译的 (因为您的消费者项目B需要较早的键入格式<= 3.6)

答案 1 :(得分:0)

解决方案:将codelyzer降级到v4.5.0