Skip to content

@kaelyx/ts-kit / object / withDefaults

Function: withDefaults() ​

withDefaults<T, D>(input, defaults): WithDefaults<T, D>

Defined in: object/with-defaults/with-defaults.ts:27

Adds default values to an options object. If a key in input is undefined, the result uses the default value for that key.

Type Parameters ​

T ​

T extends object

D ​

D extends Partial<T>

Parameters ​

input ​

T | undefined

The options object.

defaults ​

D

The default values.

Returns ​

WithDefaults<T, D>

A new object. Each key from defaults is required in its type.

Example ​

ts
const opts = withDefaults(options, { retries: 3 }); // opts.retries: number

Types ​

WithDefaults<T, D> type ​

WithDefaults<T, D> = Omit<T, keyof D> & { [K in keyof D & keyof T]-?: Exclude<T[K], undefined> }

Defined in: object/with-defaults/with-defaults.types.ts:8

Represents an object type T with default values D applied. Keys present in D are made required and their undefined type is excluded from T.

Type Parameters ​

T ​

T

D ​

D