My goal was to enhance a function that simply returns a status. It originally only accepted a logger and a message as parameters for logging purposes.
export function status(logger: Logger, reason: string) {
logger.info(reason);
return {
result: 'ok'
};
}
However, I wanted to make these parameters optional so that if no logging is needed, they do not have to be passed. This led me to modify the function like this:
export function status(logger?: Logger, reason?: string) {
reason && logger?.info(reason);
return {
result: 'ok'
};
}
Yet, I found that it was still possible to provide a logger without providing a reason, which was not what I intended. My aim was for the function to either accept both parameters or none at all. So, I attempted the following approach:
export function status(param: {logger: Logger, reason: string} | {} = {}) {
param?.logger.info(reason);
return {
result: 'ok'
};
}
Unfortunately, I encountered an error stating
TS2339: Property 'logger' does not exist on type '{} | { logger: Logger; reason: string; }'. Property 'logger' does not exist on type '{}'.
and I am unsure how to resolve this issue. Could you kindly explain how I could achieve my desired functionality?