0

我在 StackBlitz 上设置了这个示例。它不会编译,因为有一行使用 flatMap。错误说:

Property 'flatMap' does not exist on type 'string[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2019' or later.

因此,我尝试更改libto 'es2019',如 flatMap 中所建议的那样, flat, flatten 在 any[] 类型上不存在,但它并没有解决我的问题。

查看其他一些相关问题,我也尝试了'esnext'. 它也没有工作。

这是我的 tsconfig 文件:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "es6",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2019"]
  },
  "angularCompilerOptions": {
    "enableIvy": true,
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

我的 package.json 文件:

{
  "name": "angular",
  "version": "0.0.0",
  "private": true,
  "dependencies": {
    "@angular/animations": "^12.2.5",
    "@angular/common": "^12.2.5",
    "@angular/compiler": "^12.2.5",
    "@angular/core": "^12.2.5",
    "@angular/forms": "^12.2.5",
    "@angular/platform-browser": "^12.2.5",
    "@angular/platform-browser-dynamic": "^12.2.5",
    "@angular/router": "^12.2.5",
    "d3": "^4.13.0",
    "lodash": "^4.17.21",
    "rxjs": "^7.3.0",
    "tslib": "^2.3.1",
    "zone.js": "^0.11.4"
  },
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1100.4",
    "@angular/cli": "~11.0.4",
    "@angular/compiler-cli": "~11.0.4",
    "@types/jasmine": "~3.6.0",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.1.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  }
}

这是调用flatMap的代码部分:

import { Component, VERSION } from '@angular/core';
import { get } from 'lodash';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
  constructor(private d3Service: D3Service) {}
  ngOnInit() {
    const testData = [
      { name: 'Mount', count: 4, successes: 4 },
      { name: 'Sit up sweep; Reversal', count: 4, successes: 4 },
      { name: 'Turtle; Quarters', count: 3, successes: 3 },
      { name: 'No known name for this guard pass', count: 3, successes: 3 },
      { name: 'Back Step', count: 2, successes: 2 },
      { name: 'Leg lasso', count: 1, successes: 1 },
      { name: 'Lasso Sweep', count: 1, successes: 1 },
      { name: 'Americana; Keylock; Paintbrush', count: 1, successes: 0 },
      { name: 'Ezekiel', count: 1, successes: 0 },
      { name: 'Leg Drag', count: 1, successes: 1 },
      { name: 'Wrist Lock', count: 1, successes: 1 },
      { name: 'Arm Bar or Straight Arm Lock', count: 1, successes: 0 },
      { name: 'Back Control', count: 1, successes: 1 },
      { name: 'Arm Triangle', count: 1, successes: 0 },
      { name: 'Bow and Arrow', count: 1, successes: 0 },
      { name: 'Knee Slice', count: 1, successes: 1 },
      { name: 'De La Riva Guard', count: 1, successes: 1 },
      { name: 'North South', count: 1, successes: 1 },
      { name: 'Paper Cutter', count: 1, successes: 1 },
      { name: 'Half-Guard', count: 1, successes: 1 },
      { name: 'Elbow-knee Escape', count: 1, successes: 1 },
    ];
    testData['columns'] = ['name', 'count', 'successes'];
    const countCategoryArr: Array<string> = get(testData, 'columns', []).slice(
      1
    );
    const moveCounts = countCategoryArr.flatMap((entry) =>
      hist.map((d) => ({
        move: d.name,
        countCategory: entry,
        count: d[entry],
      }))
    );

  }
}

这是我在 stackBlitz 中看到的错误: 右边错误,es2019包含在主框架中

4

1 回答 1

1

我不确定确切的原因,但禁用 Ivy 可以解决它:

"angularCompilerOptions": {
    "enableIvy": false,
    ...
  }

https://stackblitz.com/edit/angular-ivy-hvpfty?file=tsconfig.json

我无法在本地重现它,所以这似乎是 stackblitz 上的 ivy 编译器中的一个错误。

于 2022-02-28T18:26:05.240 回答