Skip to content

Infer optional dependencies #144

@jackmellis

Description

@jackmellis

It'd be really nice if you could mark a dependency as optional by specifying a default value for it like so:

jpex.factory<Foo>((bar: Bar = defaultBar) => () => {});

jpex.resolve<Foo>(); // Uses default bar

This would mean if you didn't ever register a Bar factory, you could still resolve Foo and it would use defaultBar instead of throwing an error.

You could then specify dependencies but rather than having to manually create factories for them all, you could use the default value, but inject a custom dependency under test, for example.

// Some test
const stubbedBar = () => {};

jpex.constant<Bar>(stubbedBar);

jpex.resolve<Foo>(); // Uses stubbed bar

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions