Welcome Guest. Sign in or Signup

0 Comments

How to type cast in TypeScript?

Asked by: 3340 views 5.TypeScript

0
0

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.

Example:
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