2

I have a function with the ability to call out to a service and use the resulting data object response from the service to be used to generate the CRUD code necessary to interact with that endpoint. So I created an asynchronous Schematic that would call to a service, wait for that service to respond, and then continue when we were ready.

My code is something like this:

function waitingRule(options: any): Rule {
  return (tree: Tree) => {
    return new Observable<Tree>((observer) => {
      fetch('someUrl.com/') // any async operation
        .then(res => res.json())
        .then(data => {
          observer.next(tree);
          observer.complete();
        })
        .catch(((err: any) => {
          observer.error(err);
        });
    });
  };
}

This worked well until I upgraded to Angular 9. Now I get the following error:

Type '(tree: Tree) => Observable' is not assignable to type 'Rule'. Type 'Observable' is not assignable to type 'void | Tree | Rule | Observable | Promise<void | Rule>'.

In previous versions a rule can return either a Tree or an Observable, but it seems this is not possible with the last versions. Any idea of how making this working again?

igogra
  • 415
  • 1
  • 5
  • 18
  • you can use "|" to indicate that a function (or a variable) can be type of one or another type: `waitingRule(options: any): Rule|Observable{...}`. NOTE: In typescript you don't write "function" before a function – Eliseo Apr 06 '21 at 13:13
  • That doesn't work. I get the following error: "Type 'Rule | Observable' is not assignable to type 'Rule'." – igogra Apr 06 '21 at 13:52

0 Answers0