Arman
Arman

Reputation: 21

convert APP_INITIALIZER code to newer provideAppInitializer syntax

I have a piece of code from an angular application like below,

{
    provide: APP_INITIALIZER,
    deps: [...config.mockApi.services],
    useFactory: () => (): any => null,
    multi: true,
}

I want to convert it to the newer provideAppInitializer syntax.

I have come up with the below code but it throws me an error.

provideAppInitializer(() => {
    const initializerFn = ()=> (inject([...config.mockApi.services]));
    return initializerFn();
}),

Argument of type '() => unknown' is not assignable to parameter of type '() => void | Observable | Promise'. Type 'unknown' is not assignable to type 'void | Observable | Promise'.ts(2345)

Why am I getting this error, and how can I fix it?

Upvotes: 2

Views: 245

Answers (1)

JSON Derulo
JSON Derulo

Reputation: 17758

The problem is that the app initializer function is expected to return either void, an Observable or a Promise. However with your code, you are returning the injected tokens, which causes this type error.

To fix it, just make sure you are not returning the result from your injection:

provideAppInitializer(() => {
  inject([...config.mockApi.services]);
}),

Upvotes: 0

Related Questions