A simplified code example of what I'm talking about:
const err = returnTheError();
doTheThing(err); // <-- I want this to fail compilation
function doTheThing(param: number[]) {
// code that assumes param is a `number[]`
console.log("blah", param);
}
function returnTheError():any {
return Error("uh-oh");
}
Assume I can't control the fact that returnTheError
returns any
- the caller will be dealing with an any
type and there's nothing I can do about that.
I want a way to make sure nobody calls doTheThing
with an any
. If they have to do err as number[]
or something, that's fine but at the moment it's just too easy in my code-base to accidentally pass an any
on to methods that are relying on the compiler to make sure that the correct type of object is passed.
I have a TS playground showing the above example code, with all the "strict" options turned on, but still compiling.
Is there some change I can make to the definition of doTheThing
that will cause the compiler to tell the caller they haven't checked whether err
is actually the right type? Some way to say "parameter must be of type X and must not be an any
"?