TLDR: Playground Repro
In my application, I'm defining multiple form modules which look roughly like:
const firstModule = {
name: 'firstModule',
mutation: () => {
return (opts: {variables: {firstModuleArg: string}}) => {}
}
}
const secondModule = {
name: 'secondModule',
mutation: () => {
return (opts: {variables: {secondModuleArg: number}}) => {}
}
}
As you can see, each mutation function returns a function that expects a particularly shaped variables
field.
Usage of each module directly works just fine:
firstModule.mutation()({ variables: { firstModuleArg: 'test' } }); => ok
secondModule.mutation()({ variables: { secondModuleArg: 123 } }); => ok
However, I'm also creating a central registry of these forms so that I can look them up from elsewhere like so:
const forms = {
firstModule,
secondModule
}
const getFormConfig = (root: 'firstModule' | 'secondModule') => {
const rootObj = forms[root];
return rootObj;
}
This is where the issue is.. When I then try to refer to a single member of the combined form object, it seems like Typescript is automatically creating an intersection of the variables
fields and throwing the following error:
const { mutation: firstModuleMutation } = getFormConfig('firstModule');
firstModuleMutation()({ variables: { firstModuleArg: '1234' } });
I imagine I'm missing something fairly simple here, but was hoping to get some insight into how to get the ideal behavior (when I specifically retrieve the firstModule
, I only want it to validate the variables field from that module). Please let me know if there's any other information I can provide.
Thanks!