
TypeScript Fundamentals, v3
Learning Paths:
Topics:
Table of Contents
Introduction
Setup
Compiling a TypeScript Program
Mike walks through the structure of a TypeScript program, the TSC compiler CLI command, discusses how the compiler-emitted JS code changes depending on JS language level and module type, and demonstrates a program’s compiled output, including the type declaration file. A student's question regarding how to define multi-target output is also covered in this segment.Variables & Values
Mike discusses simple let and const declarations, literal types, demonstrates TypeScript throwing a type error, implicit any, and adding type annotations. Defining function arguments and return values are also covered in this segment.
Type Categories
Typing Functions
Mike demonstrates the benefits of explicitly defining return types and discusses how to narrow down the location return type errors. A student's question regarding type and test driven development is also covered in this segment.Typing Functions Q&A and Objects
Mike answers a student's question regarding why TypeScript doesn't throw an error when the result is any and why TypeScript valuable when typing can be accomplished in JavaScript. A brief overview of the properties of objects in Typescript is also provided in this segment.Optional Properties
Mike demonstrates how to state optional properties for objects using the ? operator and discusses the difference between optional properties and properties that have the option of being undefined. A demonstration of TypeScript throwing an excess property error, why the error exists, and how to handle the error is also provided in this segment.Index Signatures & Object Q&A
Mike demonstrates how to represent a type for dictionaries to allow values of a consistent type to be retrievable by keys. Student questions regarding the excess property erroring case and why storing the object as a variable removes the error are also covered in this segment.Arrays & Tuples
Mike discusses how to add a type to arrays using a pair of square brackets [ ], this can also be used to type an array using an object type. Also discussed is how TypeScript handles inference with tuples, how to state the type of a tuple, and what some of the limitations of tuples are. A student's question regarding how likely it is that JavaScript will be made a statically typed language is also covered in this segment.Structural vs Nominal Types
Mike discusses type checking as the attempt to evaluate the question of compatibility or type equivalence and the meaning of duck typing. The difference between static and dynamic type systems, nominal and structural type systems, and strong and weak types are also described in this segment.Union Types
Mike briefly discusses both union and intersection types which can conceptually be thought of as logical boolean operators (AND, OR) as they pertain to types. Union types in TypeScript can be described using the | (pipe) operator. How to narrow with type guards and a description of discriminated unions is also covered in this segment.Intersection Types
Mike demonstrates intersection types in TypeScript which can be described using the & (ampersand) operator and walks through an example of adding extra data to a Date object. A student's question regarding clarification on the difference between union and intersection types is also covered in this segment.Type Aliases
Mike provides a brief overview of the material covered Type Aliases and Interfaces segments, discusses what type aliases allow in TypeScript, and inheritance. Type aliases can define a more meaningful name for a type, declare the particulars of the type in a single place, and import and export this type from modules.Interfaces
Mike discusses interfaces which can only be used to define object types and the formalities in regards to inheritance including extends and implements. Open interfaces, how to choose which to use between type aliases and interfaces, and recursive types are also covered in this segment.JSON Types Exercise
Mike walks through an exercise of defining correct types to describe given JSON values and suppress the thrown type check errors.
Functions
Functions & Function Overloads
Mike discusses a brief overview of the functions section, the return type void, callable types, and construct signatures, which are interfaces or type aliases that describe something that can be invoked or instructed. Function overloads, where multiple function heads that serve as entry points to a single implementation are defined, are also demonstrated in this segment.this Types & Best Practices
Mike discusses how to handle and invoke a function that uses a this element, such as a click handler. A this type needs to be created in order for TypeScript to know which element this is talking about. Best practices for coding function types by explicitly defining return types is also covered in this segment.Classes & Access Modifier Keywords
Mike discusses how to define classes in TypeScript and access modifier keywords for limited exposure including public, private, and protected. JS private class fields, readonly, and param properties are also discussed in this segment.
Types & Values
Top Types: any & unknown
Mike discusses top types which describe any possible value allowed by the system with the key words any and unknown. Practical uses of top types including converting a project from JavaScript to TypeScript are also covered in this segment.Bottom Types: never
Mike discusses bottom types which describe types that hold no possible value in the system with the key word never and walks through a use case for a bottom type in an exhaustive conditional. How to handle an exhaustive conditional with an unreachable error using an error subclass is also covered in this segment.Type Guards & Narrowing
Mike discusses some of TypeScript's built-in type guards including typeof, instanceof, and property checks and demonstrates the types user-defined type guard. The two types of user-defined type guards covered in this segment include value is and asserts value is.Nullish Values
Mike discusses nullish values including null, undefined, and void, the non-null assertion operator, and definite assignment operator. Although null, void, and undefined are all used to describe “nothing” or “empty”, they are independent types in TypeScript.
Generics
Generics
Mike discusses generics which are a way of creating types that are expressed in terms of other types. A walk through of a potential use case, how to define a type parameter, and a demonstration of an example function running in TypeScript playground is also covered in this segment.Dictionary map, filter & reduce
Mike provides and then live codes an exercise to practice using generics by building out some higher-order functions that can operate on dictionaries. The functions looked at in this segment are map, filter, and reduce.Generics Scopes & Restraints
Mike discusses where type parameters can be used depending on where they are defined and minimum requirements that can be imposed on type parameters. How to describe type constraints, the scope of generics, and best practices when defining a type parameter are also covered in this segment.