Skip to content

normalizeOptionalProperties<T>

Normalizes optional properties to allow undefined assignment in strict mode.

This utility provides a workaround for the exactOptionalPropertyTypes TypeScript flag, which prohibits directly assigning undefined to optional properties when the type explicitly omits undefined.

Example:

// With `exactOptionalPropertyTypes: true`
const x: { prop?: string } = { prop: undefined }; // Compiler error
// Using this utility:
const y: { prop?: string } = normalizeOptionalProperties<{ prop?: string }>({ prop: undefined }); // Works

Import:

import { normalizeOptionalProperties } from 'obsidian-dev-utils/object-utils';

Signature:

function normalizeOptionalProperties(obj: UndefinedOnPartialDeep<T>): T

Parameters:

ParameterTypeDescription
objUndefinedOnPartialDeep<T>The object to normalize, allowing explicit undefined for optional properties.

Returns: T — The normalized object, compatible with exactOptionalPropertyTypes.