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
fix(eslint-plugin): [no-unsafe-type-assertion] fix for unsafe assertion to a constrained type parameter (typescript-eslint#10461)
* Add failing test case for unsafe type assertion with constrained generic types
* Use actual type instead of “constrained type” for no-unsafe-type-assertion
* tweak test cases
* Custom error message for “assignable to the constraint” case
* rename “assignable to the constraint” error message
* spelling
* tweak message
* Add new test cases from @kirkwaiblinger
* Move isAssignableToConstraint check into isAssertionSafe
* Properly handle unconstrained type parameters, including new error message
+ light refactor to prefer early returns over nested conditionals
* distinguish between “parameter extends other parameter” and “parameter extends unconstrained parameter”
* clean up extra pieces in unit tests
unit tests should only test one thing
* test lines
---------
Co-authored-by: Kirk Waiblinger <kirk.waiblinger@gmail.com>
Copy file name to clipboardExpand all lines: packages/eslint-plugin/src/rules/no-unsafe-type-assertion.ts
+52-19Lines changed: 52 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,6 @@ import * as ts from 'typescript';
5
5
6
6
import{
7
7
createRule,
8
-
getConstrainedTypeAtLocation,
9
8
getParserServices,
10
9
isTypeAnyType,
11
10
isTypeUnknownType,
@@ -25,8 +24,12 @@ export default createRule({
25
24
'Unsafe assertion from {{type}} detected: consider using type guards or a safer assertion.',
26
25
unsafeToAnyTypeAssertion:
27
26
'Unsafe assertion to {{type}} detected: consider using a more specific type to ensure safety.',
27
+
unsafeToUnconstrainedTypeAssertion:
28
+
"Unsafe type assertion: '{{type}}' could be instantiated with an arbitrary type which could be unrelated to the original type.",
28
29
unsafeTypeAssertion:
29
30
"Unsafe type assertion: type '{{type}}' is more narrow than the original type.",
31
+
unsafeTypeAssertionAssignableToConstraint:
32
+
"Unsafe type assertion: the original type is assignable to the constraint of type '{{type}}', but '{{type}}' could be instantiated with a different subtype of its constraint.",
0 commit comments