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

variables.js
var foo = "Hello";  // string
var bar = false;    // false
var bam = 123;      // number

// can reassign type
foo = bar; // foo is now a boolean

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.

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 used

  • lexically 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 JavaScript

  • tsconfig.json is used to configure tsc

  • The Angular CLI handles calling tsc

Summary

  • Differences between JavaScript and TypeScript

  • Classes, constructors, and interfaces

  • Modules

  • Arrow functions

  • Template Strings

  • Generics

Last updated