-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript error with async factories #83
Comments
If I may suggest simple solution. It can be done by modifying type of const createContainer = async (): AwilixContainer => {
const container: AwilixContainer = createContainer();
const config: TConfigOptions = { dbOptions: { /* ... */ } };
container.register('config', asValue(config));
// passing cradle to ensure dependencies inside factory
const db = await asyncDbFactory(container.cradle as { config: TConfigOptions });
container.register('db', asValue(db));
// rest of the code
}
// db factory
// No error anymore
const asyncDbFactory = ({ config }: { config: TConfigOptions }) =>
new Promise((resolve: (db: TDb) => void) => {
// connection logic
// resolve(db);
}) I'm aware that this is not a perfect solution, but current |
I'm fine with that. 😄 As a tip, you can actually use a double cast to get around this: container.cradle as any as { config: TConfigOptions } I'll merge your PR and release. |
[#83] Changed cradle typing into to enable casting
Released as v3.0.6, thanks! |
I've been trying to create
async
app initialization usingawilix
. Ascontainer
doesn't supportasync
factories I've used pattern recommended here, but typescript trows an error if I try to destruct dependencies in factory function declaration.Example:
It does work properly if I don't define types in
asyncDbFactory
Is there a way to bypass this that I'm not seeing or is it just a price we must pay?
The text was updated successfully, but these errors were encountered: