I am looking for typing functions with optional argument
I tried this :
type MyFunctionType<T = null> = (options: T) => unknown;
const func1: MyFunctionType<{ option1: number }> = (options) => options.option1;
const func2: MyFunctionType = () => 'test';
console.log(func1({option1: 12})); // Ok
console.log(func2()); // Error : Expected 1 argument
typescript shows an error on func2.
So i tried with "?" on options parameter:
type MyFunctionType2<T = null> = (options?: T) => unknown;
const func1: MyFunctionType2<{ option1: number }> = (options) => options.option1;
const func2: MyFunctionType2 = () => 'test';
console.log(func2()); // Ok
console.log(func1()); // No error is raised
But in this case, typescript does not show error for using func1 without parameter.
Is there a way to solve this ?
Thank you for reading