injectable-ts
injectable-ts copied to clipboard
feat(core): add optional token
Sometimes some dependencies are optional, but now there is no way to specify such dependencies correctly.
My suggestion is to add an optional token:
const foo = tokenOptional('foo')<'foo'>()
const bar = injectable(foo, (foo: 'foo' | undefined) =>
foo !== undefined ? `${foo}bar` : 'baz'
)
bar({}) // ✅ valid
@raveclassic what do u think?
@Fyzu Hm, such case should be already supported by the regular token function: const foo = token('foo')<'foo' | undefined>(), we just need to correctly produce UnknownDependencyTree with optional: true.