Programming Typescript by Boris Cherny

Created time
Nov 25, 2022 10:02 AM
Summary
Best book to learn typescript
Progress
In progress
Category
Programming
Source
Book

About Book

This book is about how to put javascript on scale with type annotations. The best book to start using Typescript so far.

Notes

Types:

  • any: try not to use it
  • unknown: just don’t
  • boolean
  • number
  • bigint
  • string
  • symbol: use it rarely, mostly for string keys in objects or maps.
  • Objects: use it for structural typing
  • Arrays
  • Typles: Tuples are subtypes of array.
  • null: universal stuff
  • undefined: Variable that has not been assigned a value yet
  • void: Function that doesn’t have a return statment
  • never: function that never returns

Declaring and Invoking Functions

// TODO

Links:

Gists

Compiler

Standard compiler:
  1. Program is parsed into an AST.
  1. AST is compiled to bytecode.
  1. Bytecode is evaluated by the runtime.
Typescript:
notion image
 
notion image
 
notion image
💡
Type literal: A type that represents a single value and nothing else.
💡
Structural typing: A style of programming where you just care that an object has certain properties, and not what its name is (nominal typing). Also called duck typing in some languages (or, not judging a book by its cover).

Intermission: Type Aliases, Unions, and Intersections

Type aliases

type Age = number type Person = { name: string age: Age }
let age: Age = 55 let driver: Person = { name: 'James May' age: age }

Union and intersection types

notion image
type Cat = {name: string, purrs: boolean} type Dog = {name: string, barks: boolean, wags: boolean} type CatOrDogOrBoth = Cat | Dog type CatAndDog = Cat & Dog // Cat let a: CatOrDogOrBoth = { name: 'Bonkers', purrs: true } // Dog a = { name: 'Domino', barks: true, wags: true } // Both a: CatAndDog = { name: 'Donkers', barks: true, purrs: true, wags: true }