Advanced Static Types in TypeScript
I'm proud to present Advanced Static Types in TypeScript, my second egghead.io course!
- 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 aboutnull
andundefined
in the type system. - 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.
- 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. - 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 forreadonly
and shows what the generated JavaScript code looks like. - 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 asObject.create
. Don't confuse it with theObject
type or{}
, the empty object type, though! - 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. - 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.
- 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. - 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.
- 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. - 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.
- 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. - 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>
orPartial<T>
that ship with the TypeScript compiler, and we'll also explore how to create our own type transformations.