Egghead course: Practical Advanced TypeScript

  • link
  • author: Rares Matei

Improve Readability with TypeScript Numeric Separators when working with Large Numbers


Make TypeScript Class Usage Safer with Strict Property Initialization

  • using the new strictPropertyInitialization property on the tsconfig.json, we can let typescript that properties on our classes are going to be initialized on construction.

Use the JavaScript “in” operator for automatic type inference in TypeScript


you can add an explicit type to an object, and infer the types of the other object properties by it. this is useful when dealing with switch statements, and make sure you are handling every case (using union types).


Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

let's explain the code above:

  • ReadonlyPet is a new type that is modifying all the properties from the IPet interface
  • it's setting all its properties to readonly (+readonly in the beginning, the + is optional)
  • it's also removing all the optional types from iPet (remove favoritePark)
  • [K in keyof IPet]: IPet[K] I guess is the mapped iterator part? 🤷‍♂️

Use Types vs. Interfaces

The main difference between type aliases and interfaces are that you can build union types with type aliases but not with interface. an Interface is an specific contract, it cannot be one thing or another.

Another thing you can do with interfaces are define different with the same name. this will result in a merge between the two. That's why you can locally extend an interface (using a typings.d.ts file for example). So make sure when you are creating a library, all the public types must be implemented with interfaces and not type aliases.


Build self-referencing type aliases in TypeScript


Use the TypeScript "unknown" type to avoid runtime errors

any type is the most loose type in TS, it will lead to lots of errors the type unknown works better because it will only accept assertions when you check types in the code


Dynamically Allocate Function Types with Conditional Types in TypeScript

You can conditionally add types to properties in your interfaces, using a ternary operator on the type declaration

You can even filter types:

Another examples is to lock down the types that a function can accept like the example below:

Generics + conditionals are super powerful


Infer the Return Type of a Generic Function Type Parameter https://egghead.io/lessons/typescript-infer-the-return-type-of-a-generic-function-type-parameter


Deeply mark all the properties of a type as read-only in TypeScript


Dynamically initialize class properties using TypeScript decorators

Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions when used correctly. In this lesson we will look at how we can use decorators to initialize properties of a class to promises that will make GET requests to certain URLs. We will also look at chaining multiple decorators to create powerful and versatile abstractions.

decorators are called from bottom to top!

© 2020, Powered by Gatsby •