I am working with a function that looks like this:
function complete<T>(result?: T): void {
}
My goal is to set a default value for result
:
function complete(result = null): void {
}
and have T
inferred as null
. This would allow me to either call
complete() // result: T = null
or
complete(valueOfSomeType) // result: T = SomeType
I attempted the following solution:
function complete<T = null>(id: string, result: T = null): void {
}
However, I encountered an error:
Type 'null' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'null'.ts(2322)
This error makes sense considering that one could call
complete<SomeType>()
How can I resolve this issue?