Below the function I currently have.
But unfortunately I got the following error:
Type ‘(T & undefined) | { obj: T & ({} | null); }’ is not assignable
to type ‘undefined extends T ? undefined : { obj: T; }’. Type ‘T &
undefined’ is not assignable to type ‘undefined extends T ? undefined
: { obj: T; }’.(2322)
function undefinedOrObject<T>( param: T ): undefined extends T ? undefined: {obj: T} {
return typeof param === 'undefined' ? param : { obj: param }
}
param
can be of type number, boolean or undefined. In case of undefined I just want to return undefined. In case of an other primitive type I would like to get an object like {obj: param}
. My goal is to solve this without any casting with as ...
. Any idea how to do this?