(TS)'MyArray []'类型中不存在属性'find'

时间:2017-09-21 16:18:17

标签: angular

我正在跟随Adam Freeman的Angular Pro一书。 我刚刚完成了第7章“SportsStrore:一个真正的应用程序”。

他在这里使用Angular 2。 (4可以下载,但试图先通过2)。

我只是从我想要解决的TypeScript编译器中得到一个小的TypeScript错误。

它显示在名为product.repository.ts的文件中。 以下是产品存储库的代码:

data.table = FALSE

这是一张Visual Studio 2017的图片,抱怨波浪形的人:

enter image description here

这是实际的tsc抱怨它:

enter image description here

我读过Array [] .find只能在ES6中使用,而不能在ES5中使用。

这是package.json文件:

fread

这是tsconfig.json文件:

import { Injectable } from "@angular/core";
import { Product } from "./product.model";
import { StaticDataSource } from "./static.datasource";

@Injectable()
export class ProductRepository {
    private products: Product[] = [];
    private categories: string[] = [];

    constructor(private dataSource: StaticDataSource) {
        dataSource.getProducts().subscribe(data => {
            this.products = data;
            this.categories = data.map(p => p.category)
                .filter((c, index, array) => array.indexOf(c) == index).sort();
        });
    }

    getProducts(category: string = null): Product[] {
        return this.products
            .filter(p => category == null || category == p.category);
    }

    getProduct(id: number): Product {
        return this.products.find(p => p.id == id);
        // return this.products.filter(p => p.id == id);
    }

    getCategories(): string[] {
        return this.categories;
    }
}

作者说,只有ES6而不是ES5支持Array [] .ref(),但大多数浏览器都支持这个,除了旧的IE。所以原始例子中有一个polyfill。

有人知道package.json文件中的polyfill是什么吗?是classlist.js吗?

然后他说,现在几天,polyfill必须直接进入tsconfig.json文件。有谁知道这条线会是什么样的?

---更新1 ---

如果我在tsconfig.json中添加这一行,它似乎摆脱了tsc错误。

{
  "dependencies": {
    "@angular/common": "2.2.0",
    "@angular/compiler": "2.2.0",
    "@angular/core": "2.2.0",
    "@angular/platform-browser": "2.2.0",
    "@angular/platform-browser-dynamic": "2.2.0",
    "@angular/forms": "2.2.0",
    "@angular/http": "2.2.0",
    "@angular/upgrade": "2.2.0",
    "@angular/router": "3.2.0",
    "reflect-metadata": "0.1.8",
    "rxjs": "5.0.0-beta.12",
    "zone.js": "0.6.26",
    "core-js": "2.4.1",
    "classlist.js": "1.1.20150312",
    "systemjs": "0.19.40",
    "bootstrap": "4.0.0-alpha.4",
    "font-awesome": "4.7.0",
    "intl": "1.2.5",
    "html5-history-api": "4.2.7"
  },

  "devDependencies": {
    "lite-server": "2.2.2",
    "typescript": "2.0.2",
    "typings": "1.3.2",
    "concurrently": "2.2.0",
    "systemjs-builder": "0.15.32",
    "json-server": "0.8.21",
    "jsonwebtoken": "7.1.9"
  },

  "scripts": {
    "start": "concurrently \"npm run tscwatch\" \"npm run lite\" \"npm run json\" ",
    "tsc": "tsc",
    "tscwatch": "tsc -w",
    "lite": "lite-server",
    "json": "json-server data.js -p 3500 -m authMiddleware.js",
    "typings": "typings",
    "postinstall": "typings install"
  }
}

完成tsconfig.json文件:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [ "node_modules" ]
}

0 个答案:

没有答案