EasyUtility Types📖 Theory Question

What are Partial, Required, and Readonly utility types?

💡

Hint

Partial makes all fields optional; Required makes all mandatory; Readonly prevents mutation

Full Answer

These three utility types transform the optionality and mutability of all properties in a type.

Partial<T> — makes every property optional (?):

interface User {
  id: number;
  name: string;
  email: string;
}

type PartialUser = Partial<User>;
// { id?: number; name?: string; email?: string }

// Common use: update/patch functions
function updateUser(id: number, changes: Partial<User>): User {
  return { ...findUser(id), ...changes };
}

Required<T> — makes every property required (removes ?):

interface Config {
  host?: string;
  port?: number;
  timeout?: number;
}

type FinalizedConfig = Required<Config>;
// { host: string; port: number; timeout: number }

// After merging defaults — all fields are now guaranteed
function buildConfig(opts: Config): Required<Config> {
  return { host: 'localhost', port: 3000, timeout: 5000, ...opts };
}

Readonly<T> — makes every property readonly:

const config: Readonly<Config> = { host: 'localhost', port: 3000 };
config.host = 'other'; // ❌ Error — readonly

// Deep readonly (not built-in, must define)
type DeepReadonly<T> = { readonly [K in keyof T]: DeepReadonly<T[K]> };
💡 Readonly is only a compile-time protection — Object.freeze() is needed for runtime. Also note: ReadonlyArray<T> is the equivalent for arrays.

More Utility Types Questions

EasyWhat are Pick and Omit utility types and when should you use them?→EasyWhat is the Record utility type and when is it useful?→EasyWhat are ReturnType, Parameters, and InstanceType utility types?→EasyWhat are Exclude and Extract utility types?→

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint