Consider the following function:
myFunc(object: string | null): string | null {}
The desired behavior for this function is to return type string
when the object parameter is of type string
, and return type string | null
when the object parameter is of type string | null
.
Attempts have been made as follows:
myFunc<T extends string | null>(object: T): T {
return "returnValue"; //Type '"returnValue"' is not assignable to type 'T'.
}
and
myFunc<T extends string & null>(object: T): T {
return "returnValue"; //Type '"returnValue"' is not assignable to type 'T'.
}
Both attempts result in the same compile error. The correct syntax has yet to be discovered.