lorenzofox3 / dismoi

MIT License
14 stars 2 forks source link

feat: validate dependency types in registry #6

Closed marcvincenti closed 1 month ago

marcvincenti commented 1 month ago

When there are type conflicts on the injectables, we get only an error when providing the api.

In this PR, we propose to typecheck the registry when creating moduleFactory. I don't see any reason (is there any ?) to inject a dependency with a different type than the one expected by other dependencies. That's why we should be able to get an error when instantiating a moduleFactory.

lorenzofox3 commented 1 month ago

Thanks Marc. I will have a look at it soon.

I don't see any reason (is there any ?) to inject a dependency with a different type than the one expected by other dependencies

A usecase which might be problematic is "optional dependencies".

You can't really declare optional dependencies, so you will need an entry in the registry for that injection token with the undefined value|type. Maybe you can declare you type in that way too but you will probably need to doublecheck your PR does not introduce a regression

const createRepository = ({session}:{session?:Session}) => {} 

const createModule = createProvider({
    injectables: {
        session: undefined // this is required or it will throw at runtime
    }
})