I have implemented a type which takes an object
and returns a type without any nullish
values.
export type OmitNullish<T> = Exclude<T, null | undefined>;
export type OmitNullishKeys<T> = {
[K in keyof T]-?: T[K] extends boolean | string | number | symbol ? OmitNullish<T[K]> : OmitNullishKeys<T[K]>;
};
However, when I attempt to retrieve a nested
key, tsc
using:
export type RandomObj = OmitNullishKeys<{
stackOverflow: {
forums?: {
thread1: 'not available';
} | null;
} | null;
}>;
export type RandomObjectAccessed = RandomObj['stackOverflow']['forums'];
it states the following:
Property 'forums' does not exist on type 'OmitNullishKeys<{ forums?: { thread1: "not available"; } | null | undefined; } | null>'.
It seems as the resulting type is treated as OmitNullishKeys
instead of an object without nullish
values. Is there a reason for this?