I am currently encountering an issue that I am unsure if it can be resolved.
function getOptions(
period: { first: string; last: string },
prefix?: string
){
if (prefix) {
return {
[`${prefix}_first`]: formatDay(period.first),
[`${prefix}_last`]: formatDay(period.last),
[`${prefix}_month`]: format(new Date(), 'MMMM', {
locale: i18n.t('datefns:format', { returnObjects: true }),
}),
}
}
return {
first: formatDay(period.first),
last: formatDay(period.last),
month: format(new Date(), 'MMMM', {
locale: i18n.t('datefns:format', { returnObjects: true }),
}),
}
}
I would like to ensure TypeScript returns the correctly typed object based on the prefix parameter. How can this be achieved?
The expected outcome is:
getOptions({first: '01', last: '10'}) // {first: string, last: string, month: string}
getOptions({first: '01', last: '10'}, 'christmas') // {christmas_first: string, christmas_last: string, christmas_month: string}