Angular CLI - 不能在Observable管道函数中使用Array.find()

时间:2018-03-14 00:36:37

标签: angular typescript ecmascript-6 angular-cli

我有以下代码:

import { AbstractControl, ValidationErrors } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { filter, map, tap} from 'rxjs/operators';


export function valueInCollectionValidator(collection: Observable<any[]>, propertyName: string) {
  return (control: AbstractControl): Observable<ValidationErrors> => {
    return collection.pipe(
      filter(array => array.find(e => e[propertyName] == control.value)),
      map(ok => ok ? undefined : { valueNotInCollection: false }),
    );
  };
}

使用angular CLI&n;服务时,我收到了构建错误:

validators.ts中的错误(9,29):属性&#39;发现&#39;类型&#39; {}&#39;。

上不存在

这是我的jsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "module": "commonjs",
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

知道为什么这不起作用?谢谢!

1 个答案:

答案 0 :(得分:1)

filter方法自动接受每个数组元素。实际上,您在array方法中引用的filter是数组collection的元素,在您的情况下是object