TypeScript
JavaScript with syntax for types: the compiler checks the shape of data and plain JavaScript comes out.
FrontendUsing since 2015 · Featured project: Budzowska Fiutowski i Partnerzy
- Category
- Frontend
- Using since
- 2015
- Featured project
- Budzowska Fiutowski i Partnerzy
In practice
I treat TypeScript as a project default, because types catch errors where data changes hands: between a form, an API and a database. On Gaia Lens the frontend moved from JavaScript to TypeScript, and the contact form in ENATIV Portfolio shares one schema between browser and server. A one-afternoon script stays plain JavaScript.
What is TypeScript?
TypeScript is JavaScript with syntax for types. Any working JavaScript is also valid TypeScript, so a project can move over gradually. The compiler checks types and removes them from the output, so the browser and the server run plain JavaScript.
The type system is structural: compatibility depends on the shape of an object, not the name of its type. Types disappear at compile time, so they check nothing while the program runs. Outside data, such as a form or an API response, must be validated separately.
- Type system
- Structural
- Types after compiling
- Removed from the output
How I work with TypeScript
I start with the places where data enters the application from outside, because that is where types without a runtime check mislead most easily. The compiler guards the inside of the code, and validation guards the borders. Inside I annotate less, at the edges more, because the compiler infers types wherever it can.
The outer border
Data from a form or an API route goes through a Zod schema, and the type follows from the schema. On ENATIV Portfolio the contact form and the API route use one schema, so they cannot drift apart.
Strict mode from day one
I switch strict mode on in the first commit and do not silence the compiler with comments. That is how ENATIV Portfolio runs: strict mode is on, and the code has no comments that switch type checking off.
Migration without a rewrite
I move existing JavaScript over gradually, starting where errors cost the most. On Gaia Lens the frontend moved to TypeScript once extending the old code kept getting heavier.
When it fits, and when it doesn't
TypeScript adds setup and learning, so I ask whether that cost pays back.
A good fit when
- An application several people will develop, or for over a year.
- A project with an API, forms and a database, where data changes shape.
- Code shared between the frontend and the backend, such as validation schemas.
- Existing JavaScript that is getting hard to change.
Frequently asked questions
Is your JavaScript code hard to change?
Describe where errors show up most often and who works in the code. I will point to the places where I would start typing, and say whether it makes sense for your project.