Skip to content
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

fix #4 #5

Merged
merged 2 commits into from
Jun 20, 2024
Merged

fix #4 #5

merged 2 commits into from
Jun 20, 2024

Conversation

lorenzofox3
Copy link
Owner

Late bound factories will receive defined injectables.

Technically they could define new dependencies which would be resolved on the late as well, but this get contrived.

@sybers
Copy link

sybers commented Jun 18, 2024

Exceptional 👏

src/index.d.ts Outdated
@@ -69,7 +69,7 @@ type ModuleAPI<Registry, PublicAPI extends Array<keyof Registry> = []> = {
type ProviderFnArgs<Registry extends ObjectLike> = {
[key in keyof ExternalDeps<Registry>]:
| ExternalDeps<Registry>[key]
| ((arg?: FlatDependencyTree<Registry>) => ExternalDeps<Registry>[key]);
| ((arg?: InjectableMap<Registry>) => ExternalDeps<Registry>[key]); // technically there is no constraint on arg, it is quite the opposite: it may bring new constraints but this gets contrived.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think arg here should not be optional

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should:

const createModule = createProvider({
    injectables:{
        someThing({random}: {random:number}) { return random * 42} 
    },
})

const myModule = createModule({
    random() {
        return Math.random();
    }
})

should be ok

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if you make it mandatory your example above is still valid as

() => number

is compatible with

(arg: InjectableMap<Registry>) => ExternalDeps<Registry>['random']

On the other hand, this is not possible:

const createModule = createProvider({
    injectables:{
        someThing: ({ random }: { random: number }) => random * 42
        randomMin: () => 100
    },
})

const myModule = createModule({
    random(injectables) => injectables?.randomMin +Math.random()
})

above, the injectables type is InjectableMap<Registry> | undefined, so we have to use the ?. operator to access the value even if we know it will be defined (also because of that we can't destructure it).

@lorenzofox3 lorenzofox3 merged commit 9a140d1 into main Jun 20, 2024
1 check passed
@lorenzofox3 lorenzofox3 deleted the fix__issue_4 branch September 22, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants