首页 > 解决方案 > Why use 'as' instead of type assertion (<>) in Typescript?

问题描述

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.

标签: typescript

解决方案


推荐阅读