You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
Once #4164 is fixed, it is not necessary anymore to cast to unknown. Implicit downcasting is always possible and should be preferred. A new rule could lint usages of as unknown and suggest implicit downcasting. I'd also like a general like to forbid casting altogether, but I think that is a separate issue.
Examples:
consta=JSON.parse(x)asunknown;// Cast not neededconsta: unknown=JSON.parse(x);// Use this instead
functiontestFunc(a: unknown): void{}testFunc(JSON.parse(x)asunknown);// Remove cast heretestFunc(JSON.parse(x));// Like this
There is one exception to this rule:
consttest={a: 'This is a string'};consttestOther=testasunknownas{b: 9};// Without casting to unknown first, this would throw an error because the types have insufficient overlap
The text was updated successfully, but these errors were encountered:
Thinking about it, this could be an extension to no-unnecessary-type-assertion. Currently the rule lints when the type stays the same, but often downcasting is also unnecessary.
Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you really need the rule, custom rules are always an option and can be maintained outside this repo!
Rule Suggestion
Once #4164 is fixed, it is not necessary anymore to cast to unknown. Implicit downcasting is always possible and should be preferred. A new rule could lint usages of
as unknown
and suggest implicit downcasting. I'd also like a general like to forbid casting altogether, but I think that is a separate issue.Examples:
There is one exception to this rule:
The text was updated successfully, but these errors were encountered: