could someone give me a hint on how to restrict the valid arguments of the function getMessage:
type Translations = {
readonly [key: string]: Localize;
}
interface Localize {
readonly "en": string,
readonly "de"?: string,
readonly "fr"?: string
}
const translationsObj: Translations = {
"keyOne": { "en":"one", "de":"eins" },
"keyTwo": { "en":"two", "de":"zwei" }
};
type TranslationKeys = keyof typeof translationsObj; // = string | number
function getMessage(key: TranslationKeys) {
return translationsObj[key]["de"];
}
const msg1 = getMessage("keyOne"); // OK
const msg2 = getMessage("notAPropOfTranslationsObj"); // should be marked as error
Only the properties of the object translationsObj should be allowed to be passed.
Roland