I am making a simple i18n system.
I have an object, which keys are language codes and values are phrases objects: phraseId: phraseText
.
type Phrase = string | ((...args: any[]) => string);
type MustHavePhrases = {
title: string,
}
type LangsParam = {
[langCode: string]: MustHavePhrases & Record<string, Phrase>;
}
export function defineLangs<T extends LangsParam>(langs: T)
{
return langs;
}
I want TypeScript to ensure exactly the same phrase keys across every language object passed to defineLangs
function:
const langs = defineLangs({
en: {
title: 'Block math',
foo: 'foo phrase',
// Error missing 'bar' property from 'ru'!
},
ru: {
title: 'Блочная формула',
bar: 'bar phrase',
// Error missing 'foo' property from 'en'!
},
// More languages (unknown amount)
});
How do I achieve this?
If it is not possible, maybe there is some hacky ways to do this, for example making the first language as main
and ensure all other languages have exact same keys as in main
one.