1

I get the following warning from my IDE because I am using TSLint.

Fix: Type assertion using '<>' syntax is forbidden. Use the 'as' syntax instead.

I understand that I should write the following:

get myCtrl(): FormControl {
 return this.formGroup.get('myCtrl') as FormControl;
}

instead of

get myCtrl(): FormControl {
 return <FormControl> this.formGroup.get('myCtrl');
}

However, are there any main issues regarding the latter one (not only grammar ambiguity as suggested in this answer)? I have up to this point always used the latter notation, so I got a bit puzzled of why we have the 'as' keyword in typescript as opposed to just keep using type assertion.

John
  • 10,165
  • 5
  • 55
  • 71
  • 5
    Because the latter would not work in `.tsx` files (it's similar to JSX syntax) – zerkms Nov 21 '19 at 08:25
  • The grammar ambiguity is the reason; if you allowed ambiguous grammar, the compiler wouldn't know how to interpret it. See [this issue](https://github.com/microsoft/TypeScript/issues/296) and the [fix introducing `as`](https://github.com/microsoft/TypeScript/pull/3201). – jcalz Nov 21 '19 at 19:45

0 Answers0