TypeScript is a transpiled language, and there is a step in the process called type erasure.
So what exactly is transpiling, and what is type erasure?
Higher Level vs Lower Level Programming Languages
Before we explain further, we have to understand higher and lower level languages.
Higher level languages are more abstracted than lower level languages. By abstracted I mean they're easier for humans to understand.
Compilation and transpilation are very similar steps, but they're not identical. I'll explain both so you know the difference.
What is Compiling?
Compiling is a catch-all term for turning code that you've written into some lower-level executable for the computer (generally machine code).
An example of some compiled languages are Java, C# or C. Sometimes it's compiled in multiple steps, each step optimising the code and getting it closer to machine code with each "pass" it does.
Through this process, a high level, closer to human readable language ends up "lower" or closer to binary.
What is Transpiling?
Transpilers are sometimes referred to as "source to source compilers" – so, a short hand way of saying "source code to source code". Transpiling means converting one higher level language to another higher level language.
The benefits of transpiling are that you can write one high level language and still end up with another high level language.
Type Erasure in TypeScript
Part of this transpiling process is called type erasure.
TypeScript code that looks like this:
let name: string = 'Kealan';
Eventually gets compiled/transpiled to this:
let name = 'Kealan'
The output might vary depending on your specific build steps (the variable may be renamed, or inlined) but the example of type erasure still stands true.
This isn't just with primitive types like
string – but even with your own custom types you may create:
type StringType = string; const firstName: StringType = "Kealan";
Type Erasure in practice
More than just conceptually understanding what type erasure is, this concept explains an important step in the transpiling process where types are thrown away and aren't used in the source code you generate.
I tweet my articles here if you would like to read more.