I'm trying to define a TypeScript type in terms of another type.
This works:
type Result = { data: { nestedData: { foo: string; bar: string } } };
type NestedData = Result['data']['nestedData'];
But, when the data
property is nullable, this doesn't work:
type Result = { data: { nestedData: { foo: string; bar: string } } | null };
type NestedData = Result['data']['nestedData'];
and results in the error:
Property 'nestedData' does not exist on type '{ nestedData: { foo: string; bar: string; }; } | null'.(2339)
How can I define my NestedData
type in terms of Result
, without duplicating any part of Result
's typings?
Edit: I'm getting my NestedData
type from a codegen tool and I'm defining NestedData
as a shorter type alias. In reality the typing is longer so I want to minimize repetition.