Skip to content

PartialKeys type ​

Makes some properties optional: the input of a factory that fills them with defaults.

ts
import type { PartialKeys } from 'utils';

Signature ​

ts
export type PartialKeys<T, K extends keyof T> = Simplify<Omit<T, K> & Partial<Pick<T, K>>>;

Parameters ​

NameDescription
TThe object type.
KThe keys to make optional.

Example ​

ts
function createUser(input: PartialKeys<User, 'id' | 'createdAt'>): User;

Tests ​

Line coverage: 0 %.

  • ✅ makes the given keys optional only

Source ​

src/types/partial-keys.ts (on GitHub)

Uses ​

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

Files it needs ​

src/types/partial-keys.ts imports, directly or not:

Source code
ts
import type { Simplify } from './simplify';

/**
 * Makes some properties optional: the input of a factory that fills them with defaults.
 *
 * @template T - The object type.
 * @template K - The keys to make optional.
 * @example
 * function createUser(input: PartialKeys<User, 'id' | 'createdAt'>): User;
 */
export type PartialKeys<T, K extends keyof T> = Simplify<Omit<T, K> & Partial<Pick<T, K>>>;
Test file
ts
import type { PartialKeys } from './partial-keys';

describe('PartialKeys', () => {
  it('makes the given keys optional only', () => {
    expectTypeOf<PartialKeys<{ a: number; b: string }, 'a'>>().toEqualTypeOf<{ a?: number; b: string }>();
  });
});

Keywords: partial, keys, types, type

Generated from the JSDoc of the sources.