I am having difficulty understanding why the compiler excludes null from the set of types possible for arg (or alternatively I do not understand the diagnostic message).
static vetStringNullable(arg:any): string|null {
if (typeof arg === 'string') {
return arg;
}
if (typeof arg === 'null') {
return arg;
}
throw TypeError('Vetted value is not of type string or null.');
}
The above code yields the following compiler diagnostic:
TS2367: This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"null"' have no overlap.
Edit: In light of the accepted answer, I have changed the function as follows:
static vetStringNullable(arg:any): string|null {
if (typeof arg === 'string' || arg === null) {
return arg;
}
throw TypeError('Vetted value is not of type string or null.');
}