injectable-ts icon indicating copy to clipboard operation
injectable-ts copied to clipboard

feat(core): add optional token

Open Fyzu opened this issue 3 years ago • 2 comments

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

Fyzu avatar May 09 '22 19:05 Fyzu

@raveclassic what do u think?

Fyzu avatar May 09 '22 19:05 Fyzu

@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.

raveclassic avatar May 12 '22 10:05 raveclassic