In this scenario, we have a function which formats a date, but also propagates nulls:
function DateToIso(d: Date | null) {
return d === null ? null : SugarDate.format(d, '%F');
}
I'd like to be able to call this function and have the return type match the nullability of the provided parameter. For example, this doesn't work – but should in my happy little world.
const nowIso: string = DateToIso(new Date());
How can I specify the function so that the above works, while still allowing propagation of nulls?