The shared DB library contains two helper functions left behind by a former senior developer.
/** Helper function to return *exactly* one result. */
export declare const one: <T>(a: T[]) => T;
/** Helper function to return zero or one result. */
export declare const first: <T>(a: T[]) => T | undefined;
While the first function works as expected, the undefined
part of the union type in the second function seems to be overlooked by the type-checker during compilation. Substituting it with void
triggers the anticipated errors where assumptions were made regarding its return value being <T>
.
I am aware that there is a distinction between undefined
and void
, as explained in this discussion on Why does TypeScript have both `void` and `undefined`?. Even though replacing undefined
with almost any other type causes errors, undefined
appears to be disregarded in a unique manner by the compiler.
This playground example showcases the error, yet I am not encountering it when importing the module into my project. What circumstances could cause the Typescript compiler to disregard an undefined
value within a union function's return type?