Flowtype nominal typing for primitive

Viewed 127

I'm looking for the best solution to separate the usage of strings as identifiers for translated messages and literal text values.

So i might have an object field called title, which in this case refers to the title of a form and it will have the value SIGNUP_FORM_TITLE or MAIL_FORM_TITLE so it will refer to a localized string and will be passed to a function like format(id) which returns the actual localized text for the current locale.

But then i might also have an object field name which refers to a person's name and is not localized, so it might have the value John Doe.

How can i make sure i get a type error when i pass name to format(id), and vice versa when i pass title to an API that expects a plain (text) string.

I can't (of course) just say type LocalizationIdentifierType = string as this is just an alias.

Maybe i could make a subclass of string class LocalizationIdentifier extends String but this would still allow LocalizationIdentifier to be passed to a string based API (or at least i think thats the case).

Has anyone a better idea to sort of nominally type a string? Or maybe i'm completely on the wrong track? ("Auf dem Holzweg" as we say in Germany)


There is a related question and a proposal to add similar functionality to typescript. Choosing typescript over flowtype is, of course, not an option.

2 Answers
Related