JavaScript is truly one of the most popular programming languages in the world. Its ability to run on both the server and the client at once is a big reason developers love it.
Its dynamic style, dynamic typing, is an advantage too. As a programmer you can write expressive code without wrestling with type declarations up front.
That freedom sometimes turns into the main enemy once the code grows large and complex. Without care, a thick pile of complex code invites runtime errors. One of the most heart-stopping ones is this:
Uncaught TypeError: Cannot read properties of undefined (reading 'val')
at :1:5
At critical moments like that, static typing and type checking feel priceless. So TypeScript arrives as JavaScript's rescuer, with a little style! ๐
Key takeaways
- TypeScript is a superset of JavaScript that adds optional static typing to cut down on bugs.
- TypeScript code gets compiled into standard JavaScript so it can run in the browser or on Node.js.
- Static types lift Developer Experience (DX) through auto-completion and errors caught early.
About TypeScript
Simply put, TypeScript is a superset of JavaScript. It carries every built-in JavaScript feature, then adds extra powerful ones. The main additions are optional static typing, type checking, class, and interface.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
TypeScript code can't run as-is. The computer has to translate (compile) it into standard JavaScript first, because browsers and Node.js only understand plain JavaScript.
Nicer still, thanks to backward compatibility, you can write plain JavaScript inside a TypeScript file. Remember, though, that doesn't work the other way around.
The headline feature is TypeScript's ability to add data types (type) to JavaScript. A type is a required property attached to every piece of data you define.
Whether on a variable, a function, a parameter, or any other object. Drop the type feature and the TypeScript you write falls back to plain JavaScript. During transpilation, those type annotations get stripped out, leaving pure JavaScript ready to execute.
๐ท Illustration: a diagram of the compile process, turning TypeScript code with type annotations into standard JavaScript.
Why Use TypeScript?
If you ask when and why you should switch to TypeScript, the answer comes back to your own needs. It all depends on project scale and your preference.
In general, though, TypeScript is worth it for medium-to-large projects whose code keeps growing. For small projects or quick experiments, plain JavaScript is honestly enough.
The proof: many modern web frameworks, like Angular, Vue v3, Nuxt 3, and NestJS, are written in TypeScript. The main reason is the static type system that suppresses runtime errors in production.
Development on the back-end (server) in the Node.js ecosystem also uses TypeScript routinely now. Deno even supports TypeScript natively, with no extra configuration.
Need more reasons to try TypeScript? Here are a few other advantages:
- Self-documented code: Types on every variable and function parameter document the code's intent. You don't have to guess which type a function expects.
- Early error detection: TypeScript code must pass through compilation first. In that phase the compiler filters problems and points at errors so you fix them before deployment. Goodbye runtime error! ๐
- Functional Programming support: Like JavaScript, TypeScript fully supports the functional style.
- Mature object-oriented programming (OOP): Concepts like abstraction, encapsulation, inheritance, and polymorphism apply cleanly, with no strange hacks.
- Editor features at full power: This is my favorite part. Neat features like auto-completion, code suggestions, error checking, and IntelliSense light up in your favorite code editor.
- A satisfying Developer Experience (DX): Writing code in TypeScript feels much more pleasant. Active IntelliSense plus code that's safer from runtime errors makes coding more confident.
From JavaScript to TypeScript
Let's look at a case. Plain JavaScript code can be confusing on the first read, mostly because of its dynamic nature. Especially if you name variables or functions carelessly, and honestly, finding good names is hard!
Look at this JavaScript snippet:
function add(a, b){
return a + b;
}
function fullName(firstName, lastName){
return firstName + " " + lastName;
}
We have two simple functions. Both take two parameters, but they mean different things. add sums numbers, while fullName joins a first name and a last name.
Now let's call both in a slightly careless way:
console.log(add(2, 4)); // output 6
console.log(add("inva", "sikode")); // output invasikode
console.log(fullName("inva", "sikode")); // output inva sikode
console.log(fullName(2, 4)); // output 24
Even with arguments aimed at the wrong target (add("inva", "sikode") and fullName(2, 4)), JavaScript won't protest and runs the code anyway, since it's syntactically valid.
That behavior is ripe for logic chaos in your app, especially when working with a large team.
TypeScript solves the problem by defining which data types a function may accept.
Now let's rewrite those functions with TypeScript syntax:
function add(a: number, b: number): number{
return a + b;
}
function fullName(firstName: string, lastName: string): string{
return firstName + " " + lastName;
}
Just by adding type annotations and changing the file extension from .js to .ts, our functions are now protected.
If we call them with the same wrong parameters:
console.log(add(2, 4)); // output 6
console.log(add("inva", "sikode")); // compile error!
console.log(fullName("inva", "sikode")); // output inva sikode
console.log(fullName(2, 4)); // compile error!
When you try to compile, TypeScript refuses outright and prints an error message. Editors like VS Code also mark the bad line with a red underline:
Argument of type 'string' is not assignable to parameter of type 'number'.
Argument of type 'number' is not assignable to parameter of type 'string'.
๐ท Illustration: a type error warning in the VS Code editor, with a red underline and a tooltip explaining the error.
This forces us to use functions by their own rules. That's a very effective way to cut down bug gaps in the projects you build.
So, interested in trying TypeScript on your next project? Head to the official site at typescriptlang.org to learn more.
Happy coding ๐