export function stringifyIfDate(date: Date | T): string | T { return date instanceof Date ? date.toISOString() : date; } type StringifyDates> = { [K in keyof T]: T[K] extends Date ? string : T[K]; }; export function stringifyDateValues>(obj: T): StringifyDates { if (typeof obj !== 'object' || obj === null) return obj; return Object.keys(obj).reduce((acc, key) => { const value = obj[key]; if (value != null && typeof value === 'object' && !(value instanceof Date)) { if (Array.isArray(value)) { return { ...acc, [key]: value.map(stringifyDateValues) }; } return { ...acc, [key]: stringifyDateValues(value) }; } acc[key as keyof T] = stringifyIfDate(value); return acc; }, {} as StringifyDates); } /** * Extracts keys from a details object where the value is null. * Used for identifying which fields should be explicitly set to null in native updates. */ export function getNullableDetailsFields>( details: Partial ): (keyof T)[] { return Object.keys(details).filter((key) => { return details[key as keyof T] === null; }); }