import type {UndefinedOnPartialDeep} from 'type-fest';
type Settings = {
optionA: string;
optionB?: number;
subOption: {
subOptionA: boolean;
subOptionB?: boolean;
};
};
const testSettingsA: Settings = {
optionA: 'foo',
optionB: undefined, // TypeScript error if `exactOptionalPropertyTypes` is true.
// @ts-expect-error
subOption: {
subOptionA: true,
subOptionB: undefined, // TypeScript error if `exactOptionalPropertyTypes` is true
},
};
const testSettingsB: UndefinedOnPartialDeep<Settings> = {
optionA: 'foo',
optionB: undefined, // `optionB` can be set to `undefined` now.
subOption: {
subOptionA: true,
subOptionB: undefined, // `subOptionB` can be set to `undefined` now.
},
};
Create a deep version of another type where all optional keys are set to also accept
undefined.Note: This is only needed when the
exactOptionalPropertyTypesTSConfig setting is enabled.Use-cases:
exactOptionalPropertyTypesis enabled, an object like{a: undefined}is not assignable to the type{a?: number}. You can useUndefinedOnPartialDeep<{a?: number}>to make it assignable.