Is there a way to convince the static analyser to narrow types based on a variable of the typeof operator.
type Arg = number | boolean | string | Date
function doString(s: string) { return s }
function doBoolean(b: boolean) { return b }
function doStuff(a: Arg) {
const t = typeof a
if (t === 'boolean') return doBoolean(a).toString()
else if (t === 'string') return doString((a as string)) // This seems futile...
else if (typeof a === 'string') return doString(a) // This is ok
else return a.toString()
}
I know that in this case you can just use a switch statement, but what if my checking is a bit more complicated ans also has a Array.isArray.