Type Inference
Blog Post
Last updated on

Type Inference


Type inference, to me, is enough of a reason to switch from using JavaScript to using TypeScript 🟨 → 🟦

While it’s generally good practice to declare types, interfaces, and/or classes in TypeScript for handling data, maintaining a type may not be so practical—like for i18n internationalization (int’l) data shown above in the screenshot.

Int’l data (1) tends to scale vertically as, e.g., new pages are added to a website and also (2) tends to scale horizontally as, e.g., new languages get added to the data, so that’s why it’s very impractical to maintain both the type and actual data for int’l.
This is where there is a need for type inference.

Type inference in TypeScript allows 2 fantastic things shown above in the code to drastically speed up development time:
(1.) Allows the use of the typeof keyword to infer the type objects for which types weren’t declared.
(2.) Allows Intellisense to provide accurate suggestions for keys, even in complex object structures.

If you’re still not convinced about the benefits of TypeScript and type inference, please feel free to play around with the code shown in the image above at the TS Playground available here.