-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
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?
knocte
Metadata
Metadata
Assignees
Labels
QuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code