I remember reading in the docs that all types are assignable to any
. But I don't remember reading that any
was assignable to every other type.
function takesAString(x: string) {
console.log(x);
}
const xAny: any = {}
takesAString(xAny); // <--- WHY IS THIS NOT AN ERROR ?
takesAString(true);
takesAString(111);
I ran into this trouble in a try-catch
loop:
catch(err) { // TYPESCRIPT EVALUATES err AS any
dispatch(SOME_ACTION(err)); // THIS EXPECTS err TO BE OF TYPE STRING
}
Typescript implicitly evaluates catch block err
argument as any
, and does not give me any heads up that SOME_ACTION(err)
might not be getting a string
as it was intended.
Is this normal behavior? How to deal with this?