Skip to content

Function: optional()

optional<K, V>(key, value): Record<K, V> | undefined

{ [key]: value } when value is present, otherwise undefined - so an absent optional field stays ABSENT when spread, rather than becoming an explicit undefined that exactOptionalPropertyTypes rejects.

Spelling that inline costs a repeated, double-evaluated ternary per field (...(v ? { key: v } : {})), which is how config resolvers end up computing the same value twice.

K extends string

V

K

V | null | undefined

Record<K, V> | undefined

return {
...optional("appId", configuredAppId),
...optional("endpoint", config.endpoint),
};