I've created a custom type that eliminates any nullish
values when working with objects.
export type ExcludeNullish<T> = Exclude<T, null | undefined>;
export type ExcludeNullishKeys<T> = {
[K in keyof T]-?: T[K] extends boolean | string | number | symbol ? ExcludeNullish<T[K]> : ExcludeNullishKeys<T[K]>;
};
However, I encountered an issue while trying to access a nested
key using TypeScript:
export type CustomObj = ExcludeNullishKeys<{
parentKey: {
childKey?: {
data: 'not available';
} | null;
} | null;
}>;
export type AccessingNestedObject = CustomObj['parentKey']['childKey'];
The error message states:
Property 'childKey' does not exist on type 'ExcludeNullishKeys<{ childKey?: { data: "not available"; } | null | undefined; } | null>'.
It seems like the compiler is treating the resulting type as ExcludeNullishKeys
rather than recognizing it as an object without any nullish
values. Any insights on this behavior?