In my base class that utilizes generics, the structure is as follows:
class Base<T extends Record<keyof T, unknown> | undefined = undefined> {
constructor(a: number, b: () => T) {
}
}
My question is if there is a way to make b
optional so that I don't have to explicitly set it to undefined
when necessary.
It's easy to achieve this with regular functions. Here's an example:
function f<T extends undefined>(a: number, b?: () => T): void;
function f<T extends Record<keyof T, unknown>>(a: number, b: () => T): void;
function f<T extends Record<keyof T, unknown> | undefined>(a: number, b?: () => T): void {
}