0

I have the following Code:

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 }),
    );
  };
}

When using angular CLI's ng serve, I get a build error:

ERROR in validators.ts (9,29): Property 'find' does not exist on type '{}'.

This is my 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"
    ]
  }
}

Any idea why this won't work? Thanks!

Will
  • 55
  • 8
  • What I was actually looking for was a way to take an observable of an array and 'flatten' it into an observable of the inidividual items. This is solved with using the mergeAll() operator and is answered here: https://stackoverflow.com/questions/42482705/best-way-to-flatten-an-array-inside-an-rxjs-observable – Will Mar 14 '18 at 03:05

1 Answers1

1

The filter method automatically takes in each array element. So in effect, the array you are referring to inside the filter method is an element of the array collection which in your case is an object.

Arjun Panicker
  • 730
  • 1
  • 8
  • 20