Skip to content

Simplify type ​

Flattens intersections and mapped types into a plain object type, so that editor tooltips show the properties instead of A & Omit<B, 'c'>. The type itself is unchanged.

ts
import type { Simplify } from 'utils';

Signature ​

ts
export type Simplify<T> = { [K in keyof T]: T[K] } & {};

Parameters ​

NameDescription
TThe type to display flat.

Example ​

ts
type Options = Simplify<BaseOptions & { isAnimated: boolean }>;

Tests ​

Line coverage: 0 %.

  • ✅ flattens an intersection

Source ​

src/types/simplify.ts (on GitHub)

Used by ​

  • Merge: Combines two object types, the properties of the second replacing those of the first (like an object spread { ...a, ...b }), where an intersection would give never for conflicting properties.
  • PartialKeys: Makes some properties optional: the input of a factory that fills them with defaults.
  • RequireKeys: Makes some optional properties required: the result of filling in defaults.
Source code
ts
/**
 * Flattens intersections and mapped types into a plain object type, so that editor tooltips show the
 * properties instead of `A & Omit<B, 'c'>`. The type itself is unchanged.
 *
 * @template T - The type to display flat.
 * @example
 * type Options = Simplify<BaseOptions & { isAnimated: boolean }>;
 */
export type Simplify<T> = { [K in keyof T]: T[K] } & {};
Test file
ts
import type { Simplify } from './simplify';

describe('Simplify', () => {
  it('flattens an intersection', () => {
    expectTypeOf<Simplify<{ a: 1 } & { b: 2 }>>().toEqualTypeOf<{ a: 1; b: 2 }>();
  });
});

Keywords: simplify, types, type

Generated from the JSDoc of the sources.