Skip to content

Instantly share code, notes, and snippets.

@cinic
Forked from palashmon/Prettify.ts
Created February 7, 2025 21:29
Show Gist options
  • Save cinic/0df1f039d7c9c4d3e15005be3e2825c0 to your computer and use it in GitHub Desktop.
Save cinic/0df1f039d7c9c4d3e15005be3e2825c0 to your computer and use it in GitHub Desktop.
A super useful type helper in TypeScript by Matt Pocock from Twitter

Use case

Suppose you have a type that has many intersections:

export type SimpleShape = {
  color: string;
} & {
  size: number;
} & {
  shape: "circle" | "square";
};

When you hover over the type SimpleShape with many intersections, it can be difficult to see the resolved type. It would be helpful if there was a way to prettify the display of these types.

With Prettify we can using:

type Shape = Prettify<SimpleShape>;
//   ^? type Shape = {
//        color: string;
//        size: number;
//        shape: "circle" | "square";
//      }

This is really helpful. Thank you, Matt!

/**
* A TypeScript type alias called `Prettify`.
* It takes a type as its argument and returns a new type that has the same properties as the original type,
* but the properties are not intersected. This means that the new type is easier to read and understand.
*/
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment