In my typescript function declaration, I have specified different arity settings:
function f(): void;
function f(code: 0): void;
function f(code: 1, msg: string): void;
function f(code: 0 | 1 = 0, msg?: string): void { /* omit implementation */ }
This allows the function to be called in various ways:
f() // valid
f(0) // valid
f(1, "error message") // valid
f(0, "message") // type error
f(1) // type error
I am interested in refactoring this function declaration without relying on function overloads. Are there alternative methods such as using union types or conditional types to achieve this?