object-transforms

Required<T>

Removes optionality from every property of T, producing a type where all fields must be present. The -? mapping modifier strips the ? flag from every key.

type Required<T> = { [P in keyof T]-?: T[P] }

Usage patterns

Pattern Purpose
Required<T> Force every property to be present after validation
Required<Pick<T, K>> Require only a specific subset of keys
-? The mapping modifier internally used to strip optionality
Required<Partial<T>> Round-trips back to a fully-required T

Examples

interface Options { host?: string; port?: number }
type ResolvedOptions = Required<Options>;

Both host and port become required: { host: string; port: number }

function resolve(o: Options): Required<Options> {
  return { host: o.host ?? 'localhost', port: o.port ?? 80 };
}

Common pattern: accept optional config, return fully-populated version

type WithId<T> = Required<Pick<T, 'id'>> & Omit<T, 'id'>;

Requires only the id field while leaving other keys unchanged

type A = { x?: number | undefined };
type B = Required<A>;

B is { x: number | undefined } — Required strips ? but not the |undefined union

Gotcha

Required removes the ? modifier but does not remove `undefined` from a property's type union. Use NonNullable on the value type if you also need to exclude undefined.

Related utility types

← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never