Monday, 28 May 2018

Angular - The $any type cast function ($any())

Sometimes a binding expression will be reported as a type error and it is not possible or difficult to fully specify the type. To silence the error, you can use the $any cast function to cast the expression to the any type.
src/app/app.component.html
<!-- Accessing an undeclared member -->
<div>
  The hero's marker is {{$any(hero).marker}}
</div>
In this example, when the Angular compiler turns your template into TypeScript code, it prevents TypeScript from reporting that marker is not a member of the Hero interface.
The $any cast function can be used in conjunction with this to allow access to undeclared members of the component.
src/app/app.component.html
<!-- Accessing an undeclared member -->
<div>
  Undeclared members is {{$any(this).member}}
</div>
The $any cast function can be used anywhere in a binding expression where a method call is valid.




No comments:

Post a Comment