TypeScript
- It is a syntactic superset of JavaScript.
- It adds static types to JavaScript.
- It compiles to readable JS.
- Three parts: Language, Language Server, and Compiler.
- Kind of like a fancy linter.
Most Browsers don’t support TypeScript. That’s why we need to compile it to JavaScript first, before shipping.
Type Annotations
Type Annotations help reduce runtime errors.
// string, number, boolean, null, undefined
const myName: string = "";
It can also infer data types, but only when the variable is initialized.
Union Types
They allow you to specify multiple data types for a variable. Use | to specify multiple types.
let myName: string | null = null;
myName = 'test';
Decorators
Decorators are functions for extending business logic or adding metadata.
To enable them in TypeScript, add the "experimentalDecorators": true option into the tsconfig.json file under "compilerOptions": {...}.
Difference b/w Expressions & String Interpolation
Expressions are the code inside the curly brackets
String Interpolation is the process of replacing placeholders into string values
"The expression {{ name }} is interpolated into John."
Backlinks