Currently, I am working on creating a type guard for my object literal to ensure the specific type of that object. Here is how my object looks:
const data = {
value: undefined as number | undefined | string,
}
Based on this setup, I believe the type of my object should be:
{value: number | undefined | string}
However, when attempting to implement a type guard using the following method:
function determineType<T>(obj: unknown, type: T): obj is T {
return true;
}
and testing it with the code snippet below:
const typedData = null;
if(determineType(typedData, data)) {
typedData
}
I noticed that the type of typedData
gets reduced to {value: number | string}
. This outcome makes me wonder why the union-types such as undefined
or null
are getting removed and if it has any significant impact.