Reputation: 64164
I am using an enum to keep translations for my code.
So for instance, I have in a file named "resources_eng" :
export enum ResourcesEnum {
TXT_YES = "yes",
TXT_NO = "no",
}
And in another file named "resources.spa":
export enum ResourcesEnum {
TXT_YES = "si",
TXT_NO = "no",
}
And finally I do in another file
import { ResourcesEnum as ResourcesEnumEng } from 'resources.eng';
import { ResourcesEnum as ResourcesEnumEsp } from 'resources.esp';
if (lang == "en") {
this.enum = ResourcesEnumEng;
} else {
this.enum = ResourcesEnumEsp;
}
But, I haven't been able to set "enum" to any type different to any, so I don't have any check.
I could have
export enum ResourcesEnum {
TXT_YES = "si",
TXT_NONONO = "no",
}
and I wouldn't get any error message.
Is there any way to give a type to my enums so that I can have an error at compile time ?
Upvotes: 0
Views: 86
Reputation: 6230
I think the approach of using enums is the incorrect one here, you will never be able to get a merged type out of the two enums because they are different, since enums don't exist in javascript they get translated to objects, please read enums
Your enum when transpiled will be:
const ResourcesEnum = {
TXT_YES: "yes",
TXT_NO: "no",
}
other approach used by apps like locize or phraseapp is something like this:
interface translation {
TXT_YES: string;
TXT_NO: string;
}
const engTranslation: translation = {
TXT_YES: 'yes',
TXT_NO: 'no'
}
Which is in my opinion what you are really looking for, you should also take a look at the apps I mentioned and/or some other i18n library.
Upvotes: 2