Skip to content

Commit 227d203

Browse files
authoredMar 15, 2025··
refactor(linter): improve typescript-no-unnecessary-type-constraint (#9798)
- Refactor `if let`s into `let ... else` for better readability through reduced nesting. - Add correctness examples to docs. Relates to #6050 .
1 parent f707d1f commit 227d203

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed
 

‎crates/oxc_linter/src/rules/typescript/no_unnecessary_type_constraint.rs

+51-21
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,46 @@ declare_oxc_lint!(
3232
///
3333
/// ### Why is this bad?
3434
///
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`.
3738
///
38-
/// ### Example
39+
/// ### Examples
40+
///
41+
/// Examples of **incorrect** code for this rule:
3942
/// ```typescript
4043
/// interface FooAny<T extends any> {}
4144
/// interface FooUnknown<T extends unknown> {}
45+
///
4246
/// type BarAny<T extends any> = {};
4347
/// type BarUnknown<T extends unknown> = {};
48+
///
49+
/// const QuuxAny = <T extends any>() => {};
50+
///
51+
/// function QuuzAny<T extends any>() {}
52+
/// ```
53+
///
54+
/// ```typescript
4455
/// class BazAny<T extends any> {
4556
/// quxAny<U extends any>() {}
4657
/// }
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+
/// }
4975
/// ```
5076
NoUnnecessaryTypeConstraint,
5177
typescript,
@@ -54,22 +80,26 @@ declare_oxc_lint!(
5480

5581
impl Rule for NoUnnecessaryTypeConstraint {
5682
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+
));
73103
}
74104
}
75105

0 commit comments

Comments
 (0)
Please sign in to comment.