Closed
Description
There doesn't seem to be a safe cast in Typescript. (<T> x) does no dynamic check. This is a very common need and is provided in every other optionally and gradually typed language I know of. What is needed is something like:
function cast<T>(x: any): T {
if (x instanceof T) return x;
throw new Error('type cast exception');
}
except that doesn't compile. Is there an alternative solution I'm missing?