I'm struggling to understand why the compiler is not including null
as a possible type for arg
, or perhaps I am misinterpreting the error 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 code above results in this compiler error:
TS2367: This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '"null"' have no overlap.
Edit: Following the answer provided, I've updated 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.');
}