I recently started hosting AMA-style cafe chats with developers at various stages of their journey, about software engineering and anything else within my technical experience. Below are some notes from the chat on Nov 16th, 2022.
Imagine you feed this to your front-end i18n library:
const i18enStrings = {
"home.title": "Life, universe, and everything",
"home.subtitle": "A guide",
};
const i18itStrings = {
"home.title": "La vita, l'universo, e tutto quanto",
"home.subtitl": "Una guida",
};
On the user side, either of these is likely to happen:
What’s more interesting though is what happens — or rather doesn’t happen — on the developer side:
And what do you fix?
The typo isn’t the problem. The problem is that all the checkpoints on the way to prod are blind to the bug.
Ask yourself:
In other words: picturing an imaginary line from dev to prod going from left to right, you want shift detection and prevention/correction as left as possible. [1]
If you can write a test to guard against a problem, good.
If you can write a type, great.
Here it’s such a small diff that someone unaware of all the above could easily dismiss it as minor. Assuming English strings are the authoritative ones regarding keys:
const i18enStrings = {
"home.title": "Life, universe, and everything",
"home.subtitle": "A guide"
}
+ type I18Strings = typeof i18enStrings
- const i18itStrings = {
+ const i18itStrings: I18nStrings = {
"home.title": "La vita, l'universo, e tutto quanto",
"home.subtitl": "Una guida"
}
Which results in:
error TS2322: Type '{ "home.title": string; "home.subtitl": string; }' is not assignable to type '{ "home.title": string; "home.subtitle": string; }'.
Object literal may only specify known properties, and '"home.subtitl"' does not exist in type '{ "home.title": string; "home.subtitle": string; }'.
8 "home.subtitl": "Una guida",
~~~~~~~~~~~~~~~~~~~~~~~~~~~