Looking to implement method overloading in Typescript with stricter type checking for method invocation based on different arguments. I am encountering an error:
'props' is possibly 'undefined'.
This is the code that is causing the error (playground):
export default class ControlsContr {
// constructor(public state: Signal<State> = signal(State.Idle)) {}
dispatch(ev: Event.Reset): void;
dispatch(ev: Event.Delete, props: { id: number }): void;
dispatch(ev: Event, props?: { id?: number }): void {
if (ev === Event.Delete) {
const num: number = props.id;
}
}
}
export enum State {
Idle = "Idle",
Deleting = "Deleting",
DeleteDone = "DeleteDone",
}
export enum Event {
Delete = "Delete",
Reset = "Reset",
}
function deleteItem(id: number) {
}
Is it possible to achieve what I'm attempting and if so, how can it be done?