Type-safe JavaScript with TypeScript
You add types to JavaScript step by step, let the compiler catch errors before the code runs and design safe interfaces with the type system.
Level 301 · 18 lessons · 15 practice labs
Lessons
Getting started
- Why TypeScript: catching errors at compile time
- tsc and tsconfig.json
- Basic types and type inference
- Function types and optional parameters
- Object types: interface and type
- Short task: add types to a JavaScript script
The type system
- Union types and narrowing
- Literal types and discriminated unions
- Generics
- Utility types: Partial, Pick, Record
- unknown, any and never
- Type guards and validation
- Short task: types for an API response
In a project
- Strict mode and strict settings
- Modules and declaration files
- Migrating a JavaScript project gradually
- Runtime validation: deriving types from a schema
- Capstone: a type-safe task manager
Practice labs
- Fix the first compile errors: Convert a discount script to .ts and fix the errors tsc reports one by one.
- Type the function signatures: Add types with optional and default parameters to shipping functions and let the compiler catch wrong calls.
- Set up tsconfig from scratch: Set up a project's tsconfig from scratch with separate src and dist, a target version and source maps.
- Write an interface for a customer record: Define an interface for a customer object with required, optional and read-only fields.
- Catch a misspelled field name: Use types so the address that goes empty because of an "adress" typo in orders gets caught.
- Build a catalogue model with intersections: Model book, notebook and pen types that share common fields using intersections and index signatures.
- Narrow a union measurement type: Narrow a measurement that arrives as string or number with typeof and format it correctly.
- Model payment states with a discriminated union: Write pending, approved and rejected as a discriminated union and let the compiler catch a missing case.
- Write a type guard for user input: Validate a JSON body that arrives as unknown with a custom type guard and use it safely.
- Write a generic first-item function: Write a generic function that works on arrays of any type and keeps the return type.
- Type an update function with Partial: Type a product-update function with Partial and Pick so it accepts only allowed fields.
- Write a type-safe event emitter: Write an event emitter using Record and generics that enforces the right payload type per event name.
- Close the null errors in strict mode: Fix the errors that appear when strictNullChecks is turned on, without needless ! assertions.
- Write a declaration file for an untyped library: Write a .d.ts file for a small local library with no types and get the calling code to compile.
- Get both type and validation from one schema: Read a config file with a type and runtime validation derived from one schema and report the bad field.
About this course
You add types to JavaScript step by step, let the compiler catch errors before the code runs and design safe interfaces with the type system.
What you will learn
- Getting started
- The type system
- In a project
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.