Goal
I am trying to create an optional parameter unless a type has been supplied making it required.
Expected Behaviour
I want the following get method to have an optional parameter by default. If the get method is called and provided a type as TT, the method should then have a required parameter of type TT.
export class Test<T = void> {
get<TT>(param: T & TT): Test<T & TT>
get<TT>(param: void | TT): Test<T & TT>
get<TT>(param: T & TT): Test<T & TT> {
return null as any;
}
}
const test = new Test();
test.get();
test
.get<{ name: string }>({ name: '' })
.get() // Expected 1 arguments, but got 0.
.get<{ age: number }>({ age: 23 }); // Property 'name' is missing in type '{ age: number; }' but required in type '{ name: string; }'
The code above will compile, but I want it to throw the errors mentioned in the comments. Any idea how I can achieve this?