Note #2

I couldn’t tell whether this would be considered coercion or transformation, so I’ll just go with “asserting on a type and return the required one”.

export const foo = (date?: string | Date): string | undefined => {}

Imagine the scenario in a JavaScript/TypeScript project where a date can be supplied as either a string or a real date object but the required end result is a string in ISO 8601 format. This code is copy pasted as-is from a project.

const publishedDate = dates && dates.published
? typeof dates.published === 'object'
? dates.published.toISOString()
: new Date(dates.published).toISOString()
: undefined;
const updatedDate = dates && dates.updated
? typeof dates.updated === 'object'
? dates.updated.toISOString()
: new Date(dates.updated).toISOString()
: undefined;

Read the rest of my notes

© Lloyd Atkinson 2024 ✌

I'm available for work 💡