I use typeof
to infer the return type of a function, but since I cannot call the actual function I use a trick using the ternary operator to infer the type, however this leaves me with a union type that includes undefined
:
function foo() {
return { bar: 1 };
}
const fooInstance = true ? undefined : foo(); // foo() is never actually called
type FooOrUndefined = typeof fooInstance; // {bar: number} | undefined
type Foo = ???; // Should be { bar: number }
Is there any way to get rid of undefined
from FooOrUndefined
?