As a novice typescript user, I am having trouble even formulating the question, so please bear with me.
I am trying to create a key => [string + valueObject interface] map of strings and valueObjects (as a type) and then have a function, which enforces the valueObject interface, based on the passed key.
I feel it's best explained by an example:
// This is an pseudo example stub, not actually working
type ReplaceableWith<T> = string;
// ^ the type I'd like to enforce as the argument
const templates = {
// templateId // template // define somehow the interface required for this template
'animal.sound': 'A {animal} goes {sound}' as ReplaceableWith<{ animal: string; sound: string}>
};
function renderTemplate(
templateId , // must be a key of templates
params // must match value object type, based on templateId
): string {
let rendered = templates[templateId];
for (const [key, value] of Object.entries(params)) {
// replace keys from template with values
rendered = rendered.replace('{' + key + '}', value);
}
return rendered;
}
const a = renderTemplate('animal.sound', { animal: 'Dog', sound: 'woof' })
// ^ a = 'A Dog goes woof'
const b = renderTemplate('animal.sound', { name: 'Some' });
// ^ should throw TS error
Obviously, this example does not work, but I think it demonstrates what I am trying to acheive. I have made some uneducated attempts with keyof, generics and enums without success.
Is this sort of type mapping (or lookup) even possible?
Update (working example)
After some playing around, here's a working example with a potential solution:
type TemplateKeys = {
'animal.sound': { animal: string; sound: string };
'animal.sleep': { location: string };
'animal.herd': { expectedCount: number; available: number };
'animal.think': undefined;
};
const templates: {[key in keyof TemplateKeys]: string} = {
'animal.sound': '{animal} goes {sound}',
'animal.sleep': 'It sleeps in {location}',
'animal.herd': 'There is {available} animals out of {expectedCount}',
'animal.think': 'Its thinking'
};
function renderTemplate<K extends keyof TemplateKeys>(key: K, params?: TemplateKeys[K]): string {
if (params !== undefined) {
//@ts-ignore
return Object.entries(params).reduce((previousValue: string, [param, value]: [string, any]) => {
return previousValue.replace('{' + param + '}', value);
}, templates[key]);
}
return templates[key];
}
console.log(renderTemplate('animal.sound', { animal: 'Dog', sound: 'woof' }));
console.log(renderTemplate('animal.sleep', { location: 'a hut' }));
console.log(renderTemplate('animal.herd', { expectedCount: 20, available: 10 }));
console.log(renderTemplate('animal.think'));
Outputs:
[LOG]: Dog goes woof
[LOG]: It sleeps in a hut
[LOG]: There is 10 animals out of 20
[LOG]: Its thinking
Although this works, it has two issues:
- I have to define keys twice (in the interface and the instance).
- The parameters interface and the message are separated, ideally they should be together.