2

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?

0

1 Answer 1

0

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]);
}),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.