Headshot of Marius Schulz
Marius Schulz Front End Engineer

Advanced Static Types in TypeScript

I'm proud to present Advanced Static Types in TypeScript, my second egghead.io course!

Advanced Static Types in TypeScript

  1. Restrict null and undefined via Non-Nullable-Types in TypeScript: This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types differ from nullable types. It also illustrates how you can write safer code by being explicit about null and undefined in the type system.
  2. Understand TypeScript’s Control Flow Based Type Analysis: The TypeScript compiler analyzes the control flow of your program to determine the most precise type for a variable at any given location. This lesson shows control flow based type analysis in action and explains how type guards and jumps in control flow affect the type that is inferred.
  3. Define Custom Type Guard Functions in TypeScript: One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a variable within a type guard. This lesson explores how you can define functions and type predicates to create your own type guards similar to the Array.isArray() method.
  4. Make Properties and Index Signatures Readonly in TypeScript: TypeScript 2.0 introduced the readonly modifier which can be added to a property or index signature declaration. It helps prevent against unintended property assignments. This lesson gives various use cases for readonly and shows what the generated JavaScript code looks like.
  5. Represent Non-Primitive Types with TypeScript’s object Type: TypeScript 2.2 introduced the object type, a type that represents any non-primitive type. It can be used to more accurately type methods such as Object.create. Don't confuse it with the Object type or {}, the empty object type, though!
  6. Use TypeScript’s never Type for Exhaustiveness Checking: TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It helps model the completion behavior of functions more accurately and can also be used for exhaustiveness checking.
  7. Overload a Function with TypeScript’s Overload Signatures: Some functions may have different return types depending on the types of the arguments with which they’re invoked. Using TypeScript’s function overloads, you can create an overload for each allowed combination of parameter and return types. This way, all type-correct signatures of a function are encoded in the type system and can be surfaced by the TypeScript Language Service within your editor.
  8. Collect Related Strings in a String Enum in TypeScript: As of TypeScript 2.4, it is now possible to define string enums, or more precisely, enums with string members. Just like any other numeric enum, string enums can be made constant using the const modifier so that they disappear entirely from the generated JavaScript; in this case, all enum values will be inlined into the output.
  9. Specify Exact Values with TypeScript’s Literal Types: A literal type is a type that represents exactly one value, e.g. one specific string or number. You can combine literal types with union types to model a finite set of valid values for a variable. In this lesson, we explore the all kinds of literal types in TypeScript: string literal types, numeric literal types, boolean literal types, and enum literal types.
  10. Model Alternatives with Discriminated Union Types in TypeScript: TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of alternative object shapes in the type system. The compiler helps you introduce fewer bugs by only exposing properties that are known to be safe to access at a given location. This lesson shows you how to define a generic Result<T> type with a success case and a failure case. It also illustrates how you could use discriminated unions to model various payment methods.
  11. Infer Types for Rest and Spread Properties in TypeScript: TypeScript supports rest and spread properties for objects, which are slated for standardization in ECMAScript 2018. It automatically infers rest and spread types so that you can use object spread and rest elements in a statically typed manner without having to manually add type annotations.
  12. Query Properties with keyof and Lookup Types in TypeScript: The keyof operator produces a union type of all known, public property names of a given type. You can use it together with lookup types (aka indexed access types) to statically model dynamic property access in the type system.
  13. Transform Existing Types Using Mapped Types in TypeScript: Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create a new type by transforming all properties of an existing type according to a given transformation function. In this lesson, we'll cover mapped types like Readonly<T> or Partial<T> that ship with the TypeScript compiler, and we'll also explore how to create our own type transformations.

This post is part of the egghead.io Video Courses series:

  1. Asynchronous JavaScript with async/await
  2. Advanced Static Types in TypeScript
  3. JavaScript Promises in Depth