user66875
user66875

Reputation: 2608

Merging two observables of the same type results in "unknown" type

I'm trying to create a function which returns an observable of an array + an optional additional element (if provided), both of type Timeformat

systemFormats: Timeformat[] = [
  new Timeformat('d/b/Y:T', "%d/%b/%Y:%T"),
  new Timeformat('Y-m-d T', "%Y-%m-%d %T"),
  new Timeformat('d.m.Y', "%d.%m.%Y"),
];

public getSystemFormats$(): Observable<Timeformat> {
  return from(this.systemFormats);
}


public getCandidates$(customFormat?: Timeformat) {    
   return merge(this.getSystemFormats$, from([customFormat]))
     .pipe(
      map(f => {    
        console.log(f.label);
      })          
    )
  }

However, Observable.merge seems to return an incorrect type, as I get the following error:

Property 'label' does not exist on type 'unknown'

I tried different ways of casting the merge result into an Observable<Timeformat> but it doesn't help.

Upvotes: 0

Views: 224

Answers (1)

DeborahK
DeborahK

Reputation: 60518

The problem is the parameter, customFormat?

public getCandidates$(customFormat?: Timeformat) {    
   return merge(this.getSystemFormats$, from([customFormat]))
     .pipe(
      map(f => {    
        console.log(f.label);
      })          
    )
  }

By adding the question mark, the result is a customFormat or empty, which loses your typing.

Try this:

     .pipe(
       filter(Boolean),
       map(f: customFormat => {    
        console.log(f.label);
      })

Or if you don't really need it, remove the ? from the parameter.

Upvotes: 1

Related Questions