Learn TypeScript Narrowing - Full Course for Free - Basic to Advanced

Опубликовано: 20 Март 2026
на канале: Lucas Paganini
5,663
108

Have you ever been in a situation where you know the type of a variable, but TypeScript doesn't? That's called Narrowing, and this is an entire course on that subject.

From the most fundamental concepts to the most advanced edge cases. We got you covered.

🚨 Subscribe to the newsletter: https://www.lucaspaganini.com/newsletter

📧 Email: [email protected]
🐦 Twitter:   / lucaspaganini  
📸 Instagram:   / lucaspaganini  

----------

CHAPTERS
1 - What is a Type Guard
0:00 Explaining the course
0:47 Introduction
1:41 What is Narrowing in TypeScript?
2:02 Why do we Need Narrowing?
2:40 What is a Type Guard?

2 - Fundamental Type Guards
4:52 The typeof Operator Guard
5:50 The in Operator Guard
7:06 Equality Narrowing
7:59 Truthiness Narrowing
8:58 Control Flow Analysis
9:51 The instanceof Operator Guard
10:16 Type never

3 - Custom Type Guards
10:49 Type Predicates
12:26 The unknown Type
12:43 Custom Guards
12:56 Error Guard
13:07 Warning Guard
13:40 Falsy Guard
14:33 formatErrorMessage() with Custom Guards
14:39 BONUS: Narrowing by Exclusion
14:56 Truthy Guard

4 - Discriminated Unions or Tagged Unions Types
15:15 Tagged Union Types (Discriminated Unions)
16:11 Discriminated Union Terminology
16:52 Type Guards for Discriminated Unions

5 - Assertion Functions or Assertion Guards
18:08 Assertion Functions vs Type Guards
19:04 Early Exits
19:52 Issues with Control Flow Analysis
20:56 Function Expressions with Predefined Types
21:38 Assertions without a Type Predicate

6 - Higher Order Guards (Functions)
23:06 Higher Order Functions
24:20 Higher Order Guards
24:49 makeIsNot Implementation
25:29 makeIsNot Signature
25:45 Type Guard Function Type
26:04 Predicate Function Type
26:31 Unpack Predicate Function Type
27:16 Back to the Signature
27:47 Library

7 - Asynchronous Type Guards
30:09 The Goal
30:58 The Workaround
32:01 Motivations for Asynchronous Type Guards

8 - TypeScript Narrowing Library
36:59 Installing and Target Audience
37:47 Currently Available Modules
38:04 Core Module
40:19 Assertions Module
41:08 Predicates Module
43:17 Series Conclusion

RELATED
1. 1min JS - Falsy e Truthy:    • Falsy and Truthy in JavaScript  
2. Null vs Undefined in JavaScript - Visually explained:    • Null vs Undefined in JavaScript - Explaine...  
3. 1min TS - Unknown vs Any in TypeScript:    • Unknown vs Any in TypeScript - With Examples  
4. Early Returns/Exits - Code Patterns in JavaScript:    • Early Returns/Exits - Code Patterns in Jav...  
5. TypeScript CONDITIONAL TYPES - Introduction to Type Functions:    • TypeScript CONDITIONAL TYPES - Introductio...  
6. TypeScript INFER Keyword- With Examples:    • TypeScript INFER Keyword - With Examples  

REFERENCES
1. TypeScript docs on narrowing: https://www.typescriptlang.org/docs/h...
2. TypeScript docs on the never type: https://www.typescriptlang.org/docs/h...
3. The instanceof operator on MDN: https://developer.mozilla.org/pt-BR/d...
4. The typeof operator on MDN: https://developer.mozilla.org/pt-BR/d...
5. The in operator on MDN: https://developer.mozilla.org/pt-BR/d...
6. TypeScript docs type predicates: https://www.typescriptlang.org/docs/h...
7. TypeScript proposal to add NaN as a type: https://github.com/Microsoft/TypeScri...
8. TypeScript docs on discriminated unions: https://www.typescriptlang.org/docs/h...
9. Assertion Functions (TypeScript Documentation): https://www.typescriptlang.org/docs/h...
10. Pull Request - Assertion Functions (TypeScript Github Repository): https://github.com/microsoft/TypeScri...
11. Pull Request - Error Messages for Assertion Functions that couldn't be Control Flow Analysed (TypeScript Github Repository): https://github.com/microsoft/TypeScri...
12. Issue - Assertion Functions and Function Expressions (TypeScript Github Repository): https://github.com/microsoft/TypeScri...
13. Higher Order Functions (Clojure Documentation): https://clojure.org/guides/higher_ord...
14. Functional Programming - Predicate Functions (Stanford Education): https://dcl-prog.stanford.edu/functio...
15. TypeScript Utilities Library - @lucaspaganini/ts (Github Repository): https://github.com/LucasPaganini/ts
16. Feature Request - Asynchronous Type Guards and Assertion Signatures (TypeScript Github Repository): https://github.com/microsoft/TypeScri...
17. My Comment Explaining this Workaround (TypeScript Github Repository): https://github.com/microsoft/TypeScri...

#typescript #programming #webdevelopment