Using utility types
TypeScript comes bundled with several utility types to facilitate common type transformations. Almost any time you want to define types and interfaces, you also want to consider whether it's better to apply them together or create custom types to enforce some constraints.
Let's take a look at some examples. Imagine that you define a model for a User
type that represents a database entity. You would normally define a type like this:
Utilities.ts
type Address = { streetNum: number; town: string; streetName: string; } type User = { name: string; email: string; address: Address; } function loginUser(user: User, password: string) { // perform login }
Initially, this may work, but later on, you might want to make...