10

为什么角度编译器找不到GeolocationPositionand GeolocationPositionError?VSC 不会出错,只有在编译时才会出错。

Error: src/app/modules/shared/services/position.service.ts:10:46 - error TS2304: Cannot find name 'GeolocationPosition'.

10   private positionSource = new ReplaySubject<GeolocationPosition>(1);

我通过放置any类型来解决这个问题,但我只是好奇为什么 Angular 会给我错误以及如何解决这个问题。

我已经尝试过不同的编译器目标(es5, es6, es2018),但没有运气。已经安装@types/core-js但也没有任何运气。我可能缺少任何@types 模块吗?

这是我当前的 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es5",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

4

2 回答 2

18

In typescript 4.0.x this type was called Position. In typescript 4.1.x it was renamed to GeolocationPosition.

I think you'll find that your VS Code is using its built in version of typescript and that your project is using an earlier version.

To fix:

npm install --save-dev typescript@4.1.2
于 2021-02-02T07:51:16.837 回答
0

我遇到了同样的问题,我解决了更改项目使用的本地 Angular CLI。
说明
我更新到 Angular 11,但我忘记更新 Angular CLI。给ng build了我这个输出:

Your global Angular CLI version (9.1.7) is greater than your local
version (9.1.0). The local Angular CLI version is used.

步骤
更新 Angular CLI(如有必要)

npm install -g @angular/cli

更新 npm

npm install -g npm

然后ng build输出变为:

Your global Angular CLI version (11.1.2) is greater than your local version (9.1.0). The local Angular CLI version is used.

安装 npm-check-updates

npm i -g npm-check-updates

执行ncu -u以查看要更新的包。
执行npm install以更新所有包。
在此之后,一切都应该工作。

于 2021-02-01T17:50:01.413 回答