Beginners Guide To TypeScript
What is TypeScript ⁉️
I am new to TypeScript and just started reading the book Tackling TypeScript ( It is donation base 😄 ). TypeScript can look intimating at first and can add complexity to your code, but done correctly it can be very powerful!
I thought it would be a great way for me to learn by making blog posts as I read my way through Tackling TypeScript. This is the very first article, which will just be a basic overview of TypeScript and how it works with JavaScript.
But what is TypeScript really…
It is a typed programming language that builds on JavaScript. It has many benefits but mainly it can identify behavior that leads to errors and bugs in your code!
How does TypeScript Work
TypeScript works by adding syntax to JavaScript and then transforming it back to JavaScript after the TypeScript compiler does its type checks. Pretty much meaning the compilers job is to type check and assemble all TypeScript files into JavaScript files.
You can customize how the TypeScript compiler checks your code through the file tscongig.json
. It is best practice to set the strict
flag as true
. This tells the compiler how in depth to check your TypeScript code. To see exactly what the flag does checkout this article.
JavaScript Types & Types in TypeScript
Javascript has 8 types, a type is pretty much a set of values. JavaScript types are made up of primitive values
and objects
.
The 8 types are …
undefined
null
boolean
number
bigInt
string
symbol
objects
(includes arrays)
All 8 types are dynamic types, meaning all type checking is done at runtime. This can cause problems since variables can be assigned/re-assigned values of any type and the code will compile even if there are errors. But do not worry, this is where TypeScript comes to the rescue!
TypeScript brings us static types, which is when the compiler checks that the values use the correct type. This prevents bugs and errors before runtime 🥳.
Thanks for learning with me! I will be back soon with my next article, where I will actually start writing some TypeScript and discussing the basics.