TypeScript is a strongly typed programming language that builds on JavaScript.
Documentation on JavaScript at the Mozilla Web Docs.
Strongly typed vs Loosely typed
The terms strongly typed
and loosely typed
refer to how programming languages handle types, particularly how strict they are about type conversions and type safety.
Strongly typed languages | Loosely typed languages |
---|---|
1. Examples - Java, C++, C, Rust 2. Benefits - 1. Lesser runtime errors 2. Stricter codebase 3. Easy to catch errors at compile time | 1. Examples - Python, Javascript, Perl, php 2. Benefits 1. Easy to write code 2. Fast to bootstrap 3. Low learning curve |
Does not work
#include <iostream>
int main() {
int number = 10;
number = "ten";
return 0;
}
Does work
function main() {
let number = 10;
number = "text";
return number;
}
What is typescript?
TypeScript is a programming language developed and maintained by Microsoft.
It is a strict syntactical superset
of JavaScript and adds optional static typing to the language.
Where/How does typescript code run?
Typescript code never runs in your browser. Your browser can only understand javascript
.
-
Javascript is the runtime language (the thing that actually runs in your browser/nodejs runtime)
-
Typescript is something that compiles down to javascript
-
When typescript is compiled down to javascript, you get
type checking
(similar to C++). If there is an error, the conversion to Javascript fails.
Typescript compiler
tsc
is the official typescript compiler that you can use to convert Typescript
code into Javascript
There are many other famous compilers/transpilers for converting Typescript to Javascript. Some famous ones are -
-
esbuild
-
swc
How to create a ts project
Step 1 - Install typescript globally
npm install -g typescript
Step 2 - Initialize an empty Node.js project
mkdir node-app
cd node-app
npm init -y
Step 3 - Initialize a tsconfig.json
file.
npx tsc --init