@@ -32,20 +32,46 @@ declare_oxc_lint!(
32
32
///
33
33
/// ### Why is this bad?
34
34
///
35
- /// Generic type parameters (`<T>`) in TypeScript may be "constrained" with an extends keyword.
36
- /// When no extends is provided, type parameters default a constraint to unknown. It is therefore redundant to extend from any or unknown.
35
+ /// Generic type parameters (`<T>`) in TypeScript may be "constrained" with an `extends`
36
+ /// keyword. When no `extends` is provided, type parameters default a constraint to `unknown`.
37
+ /// It is therefore redundant to `extend` from `any` or `unknown`.
37
38
///
38
- /// ### Example
39
+ /// ### Examples
40
+ ///
41
+ /// Examples of **incorrect** code for this rule:
39
42
/// ```typescript
40
43
/// interface FooAny<T extends any> {}
41
44
/// interface FooUnknown<T extends unknown> {}
45
+ ///
42
46
/// type BarAny<T extends any> = {};
43
47
/// type BarUnknown<T extends unknown> = {};
48
+ ///
49
+ /// const QuuxAny = <T extends any>() => {};
50
+ ///
51
+ /// function QuuzAny<T extends any>() {}
52
+ /// ```
53
+ ///
54
+ /// ```typescript
44
55
/// class BazAny<T extends any> {
45
56
/// quxAny<U extends any>() {}
46
57
/// }
47
- /// const QuuxAny = <T extends any>() => {};
48
- /// function QuuzAny<T extends any>() {}
58
+ /// ```
59
+ ///
60
+ /// Examples of **correct** code for this rule:
61
+ /// ```typescript
62
+ /// interface Foo<T> {}
63
+ ///
64
+ /// type Bar<T> = {};
65
+ ///
66
+ /// const Quux = <T>() => {};
67
+ ///
68
+ /// function Quuz<T>() {}
69
+ /// ```
70
+ ///
71
+ /// ```typescript
72
+ /// class Baz<T> {
73
+ /// qux<U>() {}
74
+ /// }
49
75
/// ```
50
76
NoUnnecessaryTypeConstraint ,
51
77
typescript,
@@ -54,22 +80,26 @@ declare_oxc_lint!(
54
80
55
81
impl Rule for NoUnnecessaryTypeConstraint {
56
82
fn run < ' a > ( & self , node : & AstNode < ' a > , ctx : & LintContext < ' a > ) {
57
- if let AstKind :: TSTypeParameterDeclaration ( decl) = node. kind ( ) {
58
- for param in & decl. params {
59
- if let Some ( ty) = & param. constraint {
60
- let ( value, ty_span) = match ty {
61
- TSType :: TSAnyKeyword ( t) => ( "any" , t. span ) ,
62
- TSType :: TSUnknownKeyword ( t) => ( "unknown" , t. span ) ,
63
- _ => continue ,
64
- } ;
65
- ctx. diagnostic ( no_unnecessary_type_constraint_diagnostic (
66
- param. name . name . as_str ( ) ,
67
- value,
68
- param. name . span ,
69
- ty_span,
70
- ) ) ;
71
- }
72
- }
83
+ let AstKind :: TSTypeParameterDeclaration ( decl) = node. kind ( ) else {
84
+ return ;
85
+ } ;
86
+
87
+ for param in & decl. params {
88
+ let Some ( ty) = & param. constraint else {
89
+ continue ;
90
+ } ;
91
+
92
+ let ( value, ty_span) = match ty {
93
+ TSType :: TSAnyKeyword ( t) => ( "any" , t. span ) ,
94
+ TSType :: TSUnknownKeyword ( t) => ( "unknown" , t. span ) ,
95
+ _ => continue ,
96
+ } ;
97
+ ctx. diagnostic ( no_unnecessary_type_constraint_diagnostic (
98
+ param. name . name . as_str ( ) ,
99
+ value,
100
+ param. name . span ,
101
+ ty_span,
102
+ ) ) ;
73
103
}
74
104
}
75
105
0 commit comments