Welcome Guest. Sign in or Signup


How to type cast in TypeScript?

Asked by: 3148 views 5.TypeScript


You can use a “type assertion” expression to do so. A type assertion expression consists of a type enclosed in < and > followed by a unary expression. Type assertion expressions are purely a compile-time construct. Type assertions are not checked at run-time and have no impact on the emitted JavaScript (and therefore no run-time cost). The type and the enclosing < and > are simply removed from the generated code.

var x= < SomeType > e;
Among other things, the above code will also make the editor give appropriate intellisense to x.
Type assertion is well explained (in the section 4.13 Type assertions) here.
Here is another link which deals with type assertion
Here is an example for type assertion.

Comments on Question