🔧 TypeScript Utility Types

TypeScript can be super useful. Its type checking can reduce runtime errors during development and help solve codebase scalability challenges. Personally, I love TypeScript. At least, most of the time. Occasionally I get frustrated with a failing build due to a type issue, even though the code works just fine.

Fortunately, there exists some pretty awesome Utility Types, which I wish I had learned about sooner. With these built-in types, it is easier to overcome these types (no pun intended) of issues and they work better than using any or unknown.

Below are a few of my favorites.


Record<Keys, Type>[1]

Not every data structure in my code has an explicit type definition or a finite list of keys. In these situations Record is a nice utility that generates a dynamic type with constrains for the keys and values of an object.

// before
const flags = {};
flags.myFeature = true;
// ✖ī¸ Property 'myFeature' does not exist on type '{}'

// after
const flags: Record<string, boolean> = {};
flags.myFeature = true;
// ✔ī¸ valid!



Make all properties of an object optional. Super useful, especially since the alternative is usually code duplication.

// before
const updatePost = (id: number, post: Post) => { ... };
updatePost(1, { name: 'My New Name' });
// ✖ ...is missing the following properties from type 'Post': id, content.

// after
const updatePost= (id: number, post: Partial<Post>) => { ... };
updatePost(1, { name: 'Twitter Post #1' });
// ✔ī¸ valid!


Parameters[3] ConstructorParameters[4] ReturnType[5]

This is perfect for when a type definition doesn't exist or can't otherwise be imported.

// some-package/index.d.ts
type InternalOptions = ...; // not exported!
type InternalResults = ...;  // not exported!
export function config(options: InternalOptions) : InternalResults;
// app.ts
import { config } from 'some-package';
type ConfigOptions = Parameters<typeof config>[0];
const myConfig: ConfigOptions = { ... };
// ✔ī¸ valid!

type Results = ReturnType<typeof config>;
let myResult: Results;
myResult = config(myConfig);
// ✔ī¸ valid!


These are the utilities that I use most frequently and they make it more convenient to work with TypeScript. I recommend reading through then entire list and seeing what built-in types could simplify your life.

  1. TypeScript Utility Types: Record ↩ī¸Ž

  2. TypeScript Utility Types: Partial ↩ī¸Ž

  3. TypeScript Utility Types: Parameters ↩ī¸Ž

  4. TypeScript Utility Types: ReturnType ↩ī¸Ž

  5. TypeScript Utility Types: ConstructorParameters ↩ī¸Ž