TypeScript

Introduction

Built-in types

Compiler options

Conditional types

  • 📺 Conditional Types, TypeScript's most POWERFUL feature • 4 minute video by Basarat Ali using the example of a typeName helper to demonstrate how conditional types work and why you can think of them as functions that compute an output (a specific type) based on an input (a generic type)
  • 📺 TypeScripts amazing INFER Keyword • 3 minute video by Basarat Ali showing how to use the infer keyword inside a conditional type to get the type name of the generic argument that was passed in

Generic types

  • 📺 Why Generics? • 3 minute video by Basarat Ali showing how generics help in cases where a structure accepts alternating types of inputs and outputs

Literal types

Special types

  • 📺 Lookup Types - one of the first super powers of TypeScript • 3 minute video by Basarat Ali showing how lookup types can clean up your code and prevent typos by using a larger root type as the source of truth for smaller types
  • 📺 What are TypeScript MAPPED Types and how to use them Effectively • 4 minute video by Basarat Ali using the example of creating a readonly version of an existing type to demonstrate how a mapped type is created by mapping over the properties of an existing type and modifying their definitions (similar to creating a new JS array by mapping over an existing array and returning a copy that transforms each item in some way)
  • 📺 Mapped type MODIFIERS in TypeScript • 4 minute video by Basarat Ali showing how to use +, - and Partial to modify properties in a mapped type

Operators

Type guards

Narrowing

  • Narrowing types at run-time with typeof and instanceof (really a JS thing, but useful in TS too)
  • Narrowing TS types with arg is Type type predicate functions
    • e.g. function isString(arg: unknown): arg is string { return typeof arg === 'string'; }
    • e.g. function isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined; } (copied from docs)
  • Narrowing TS types with switch statements
  • Narrowing TS types with function overloads (when the return type of a function changes as the type of its arguments change)

Inbox