Here is an example of my function:
const safeIdCastToNumber = (id: string | null | undefined) => isNil(id) ? id : Number(id)
When calling safeIdCastToNumber, I can use an id parameter with a type union string | null | undefined
, as well as one with a type union string | undefined
for non-nullable ids.
The return type of this function is number | null | undefined
.
I want the return type to exclude null
if the id parameter does not include null
.
For instance, if using an id parameter of type string | undefined
, the return type should be number | undefined
. However, if utilizing an id parameter of type string | null | undefined
, the return type should be number | null | undefined
.
Is it feasible to achieve this?