简体   繁体   中英

How to type an observable using an async pipe within Angular

I am trying to type an observable that utilizes the async pipe. Right now I am currently getting the error:

Property 'meatCount' does not exist on type 'unknown'.
<ng-template let-wowzers>
  <my-component
    class="my-component-class"
    [myProp]="(wowzers.testObservable$ | async)?.meatCount"
  >
  </my-component>
</ng-template>

The async pipe returns an object of type: Store

class Store {
   meatCount: number;
}

I know that there is a workaround where we would simply do something like:

<ng-template let-wowzers>
  <my-component
    class="my-component-class"
    [myProp]="getTypeStore(wowzers.testObservable$ | async)?.meatCount"
  >
  </my-component>
</ng-template>

Where the getTypeStore would return something like so:

  public getTypeStore = (store: Store): Store => store;

But is there another way to more effectively handle this problem? I am a little lost on how to type the observable using the async pipe within the html page within Angular?

Have you tried giving a type to your observable in the component

wowzeers: {testObservable$: Observable<Store>} | undefined;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM