port @typescript-eslint/consistent-type-assertions #8722
Closed
redbmk
started this conversation in
Rule suggestion
Replies: 1 comment
|
Hey @redbmk! I've added |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I often find it dangerous to use
return x as Xas it signals to typescript that you know more than it does. That might be fine for smaller libraries but on larger projects refactoring can be sketchy if you can't trust the compiler.There's an eslint plugin
@typescript-eslint/consistent-type-assertionsthat can not only enforce style (e.g.<Type>thingvsthing as Type) but can also prevent you from using it at all.The formatter could handle enforcing consistency. For example if you have
assertionStyle: 'angle-bracket':becomes
The linter could also ensure that you don't force types. Examples of errors:
Examples of valid code (
as constis still OK):In some cases typescript will still raise an error when trying to cast something that doesn't look correct but a real-world example where it wouldn't catch it is a fetch call where
json()returnsPromise<any>. Typescript will let you cast it without doing any actual validation:It would be safer to use a validation library (e.g. zod) for any/unknown/untrusted data.
All reactions