-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat(types): functions must be wrapped in functions #7
feat(types): functions must be wrapped in functions #7
Conversation
src/index.d.ts
Outdated
/** | ||
* Factory as defined in the injectable property: either a function with eventually a single named dependencies argument object or a value | ||
*/ | ||
export type FactoryFn<FactoryLike> = FactoryLike extends (args: any) => any | ||
export type FactoryFn<FactoryLike> = FactoryLike extends Function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is not quite the same. Factories have only one argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i took an unnecessary shortcut by sharing a generic Function
type. I removed it from this PR.
Edit: and wrong shortcut
Looks good Marc. Thanks for the contribution |
Thank you for your review and all your propositions. 🙏 |
EDIT: fixed and I added a test for this case I'm noticing a weird behavior when a provider require a dependency being directly an union type (it works fine with a function that return an union type). I cannot inject the dependency wrapped in a function because of a weird TS behavior that split the type of Example: createProvider({
injectables: {
foo: ({ bar }: { bar: 'a' | 'b' }) => bar,
bar: (): 'a' | 'b' => 'a', // <-- Type '() => "a" | "b"' is not assignable to type '"b" | "a" | ((deps?: any) => "b") | ((deps?: any) => "a")'.
},
api: ['foo'],
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why you use an array. But, indeed, your test with the union fails if you don't 😅
When a provider require an injectable extending a function signature, this function needs to be wrapped by another function since the module will automatically call the former function to instanciate the injectable.
Dependencies can be of type
T | (args?: any) => T
, we propose to narrow this type to only(args?: any) => T
when T is a function.