Since TypeScript 3.0 the new top type unknown is available as a type-safe counterpart of any.
I really love this type, as it makes the code even more robust.
My main problem with this is, that TypeScript (obviously) does not provide an instanceof
-feature at runtime for interfaces. If it would (be possible), this code would work quite well:
class Test {
doSomething(value: unknown) {
if(value instanceof MySuperInterface) {
return value.superFunction();
}
return false;
}
}
My question
How do I use this type in a meaningful way, without the need of checking each property of my interface?