When working with a JavaScript library, I encountered an issue where I needed to define my callback functions within an object. The goal was to include default parameters in the arguments of these callback functions stored in a TypeScript object. Here is a simplified example:
type Info = {
setStatus: (newStatus: string | null) => void;
};
let status: string | null = null;
const info: Info = {
setStatus: (newStatus: string | null = null) => {
status = newStatus;
}
};
info.setStatus();
Despite using a default parameter in the callback function,
info.setStatus();
<----- expects argument
Expected 1 arguments, but got 0.
An argument for 'newStatus' was not provided.
https://i.sstatic.net/bOlHL.png
This situation reveals that TypeScript expects an argument even when a default parameter is set. Is there a way to inform TypeScript to acknowledge and use the default parameter in the callback function's argument?