I’m trying to pass a generic type argument using typeof
. The variable may be undefined, but the generic type argument does not allow undefined.
// T cannot be undefined
type SomeType<T extends object> = {
// ...
};
// variable may be undefined
const data: object | undefined = undefined;
type OtherType = SomeType<typeof data>;
// Error ^
// Type 'undefined' does not satisfy the constraint 'object'
How can I get around this error, so that OtherType
is either undefined or the returned type from SomeType<T>
? I thought this might be the answer, but I still get the same error.
type OtherType = typeof thing extends undefined
? undefined
: SomeType<typeof thing>;