TypeScript
What is TypeScript
Superset of JavaScript
Strongly typed
Supports Object Oriented design
Angular is written in TypeScript
Easy transition from JavaScript -- looks very similar
Similar to strongly typed languages like C# and Java
Can auto upgrade transpiled code as browsers support new features
Can be used on any project type where JavaScript is used.
JavaScript vs TypeScript
Variables
Arrays
Types in Functions
Inference
TypeScript is smart and can infer types based on what is assigned.
Specifying types makes your code more readable and will make refactors later much easier.
Avoid: Use the any type sparingly and only as a last resort. It makes your code less reliable. Consider using a pipe operator if your variable needs to support multiple types.
var foo: string | number; // accepts strings or numbers
Classes
TypeScript supports classes and can be used to create complex data structures
Class Constructors
Class Constructor - Public params
Declaring parameters as public on the constructor dynamically adds a public variable to the class and no assignment is necessary.
Classes also support private and protected scoping on variables and work just like any other strongly typed language
Interfaces
TypeScript supports interfaces and enforces interface contracts. If a class implements an interface it has to implement all of the properties and methods of the interface.
ES6 Modules
TypeScript supports exporting classes, interfaces, modules, functions, and variables to be used in other files.
Var, Let, Const?
What is the difference and when do I use which?
Var
Part of JavaScript and supported by TypeScript
Are scoped to a function
Are easily created as globals and have bad consequences
Let
Introduced in TypeScript
Prefer over Var
Block scoped
Const
Only assigned once
Cannot be reassigned
An object's properties can be reassigned
Arrow Functions
Otherwise known as fat arrow functions and sometimes called a lambda function.
Don't have to keep typing function
The
this
value of the enclosing lexical scope is usedlexically captures meaning of arguments
Commonly used with RxJs
Template Strings
String concatenation can be messy and slow. Consider this:
Uses back-tick character to indicate string
Tokens are replaced with variable values, i.e., ${variable-name}
Can be multiline
Generics
Allows functions and classes to work with a variety of types instead of just one. This concept is borrowed from languages like C# and Java.
The Angular HttpClient uses generics to enforce the type returned by our API call.
Generics with Types
Sometimes our functions need to enforce that the parameter follows a given contract.
TypeScript Transpilation
Browsers don't understand TypeScript, so it must be transpiled to JavaScript.
tsc
transpiles TypeScript to JavaScripttsconfig.json
is used to configuretsc
The Angular CLI handles calling
tsc
Summary
Differences between JavaScript and TypeScript
Classes, constructors, and interfaces
Modules
Arrow functions
Template Strings
Generics
Last updated