awilix: How to type a container that has a factory function returning a Promise?

It is my understanding that awilix will do the right thing and resolve the Promise before resolving the dependency.

Am I assumption correct?

In, which case it is not clear how to type this without failing type checks:

import { asFunction, createContainer } from 'awilix'

interface Container {
  foo: boolean
}

const factory = () =>
  new Promise<boolean>((resolve) => {
    return resolve(true)
  })

const container = createContainer<Container>()

container.register({
  // this fails
  foo: asFunction(factory),
})

In this case, foo in registration is failing the type check:

Type 'BuildResolver<Promise<boolean>> & DisposableResolver<Promise<boolean>>' is not assignable to type 'Resolver<boolean> | undefined'.
  Type 'BuildResolver<Promise<boolean>> & DisposableResolver<Promise<boolean>>' is not assignable to type 'Resolver<boolean>'.
    The types returned by 'resolve(...)' are incompatible between these types.
      Type 'Promise<boolean>' is not assignable to type 'boolean'.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

I’m going to close this as I settled on the async factory function, like you suggested. Thanks.