pietergreyling / programming_notes_typescript

Programming Notes - TypeScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript Programming Notes

These notes help to aid my short term memory.

Installation

Install NPM/NodeJS

Installing NPM and Node.js via a package manager

Installing NPM and Node on Ubuntu Linux

$ sudo apt update
$ sudo apt install nodejs npm

Installing NPM and Node on macOS with Homebrew

$ brew install node

Checking your version of npm and Node.js

$ node -v
$ npm -v

Install TypeScript

Installing TypeScript globally

npm install -g typescript
  • Linux
sudo npm install -g typescript

Installing TypeScript into your current project

$ npm install typescript --save-dev
  • Checking your TypeScript version
$ tsc -v                        

Types, Interfaces, Unions, Enums, Classes!

Types

  • a type is low level
  • types are closed
  • types cannot be augmented
  • types can be composed into a new type

Extending types

type ToDoListItem = {
  title: string;
  completedDate: Date | null;
}
type ToDoList = {
  todos: ToDoListItem[];
}
type CalendarEvent = {
  title: string;
  start: Date;
  end: Date;
}
type Calendar = {
  events: CalendarEvent[];
}
type AppState = ToDoList & Calendar & {
  modified: Date;
}

Interfaces

  • an interface is high level
  • interfaces are open
  • interfaces can be augmented
  • interfaces can be merged and remain the same interface
  • interfaces can implement multiple interfaces

Unions

  • A simple union:
type Union = "X" | "Y" | "Z";

Enums

enum Direction {
  Up = 1,
  Down,
  Left,
  Right,
}

Classes

  • classes are high level
  • a class can implement interfaces

Resources

TypeScript Programming Language

The TypeScript Handbook

The TypeScript Online Playground

Unions and Intersection Types

Type vs Interface in TypeScript

Exploring the satisfies operator in TypeScript

Extending object-like types with interfaces in TypeScript

About

Programming Notes - TypeScript

License:MIT License