I`m trying to define function with 2 args: (templateKey: T, templateData: ObjectType) => ... where templateKey is a well known mapped value and templateData is data specified for this key. As result i want one function to work with all templates from one place.
I've tried this:
export const Template = [
'CASE1',
'CASE2',
] as const;
type RequestDTO = {
test: string,
}
type RequestDTO2 = {
jest: string,
}
type ObjectType<T extends TemplateKey> =
T extends 'CASE1' ? RequestDTO :
T extends 'CASE2' ? RequestDTO2 :
never;
export type TemplateKey = typeof Template[number];
const func = async <T extends TemplateKey>({ template, templateData } : { template: T, templateData: ObjectType<T> }) => 1;
const U = func({ template: 'CASE1', templateData: { test: '123' } });
All works fine for me - hints are where they must be and this way pretty convenient. But! I see one awkward thing: no typehints when i write new rule in T extends 'CASE2' ? RequestDTO2 and when number of templates grows, there will be huge ObjectType ruleset with rows of ternary operators without typehitting... But i have no idea how to do it without a map or something like map and im a bit confused.
Have you any ideas about how to optimize this or what i did wrong?