Skip to content

Commit 4fac6c5

Browse files
Retsambradzacher
authored andcommitted
fix(eslint-plugin): [no-unnecessary-cond] fix naked type param (typescript-eslint#1207)
1 parent 259ff20 commit 4fac6c5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/eslint-plugin/src/rules/no-unnecessary-condition.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,16 @@ export default createRule<Options, MessageId>({
110110
function checkNode(node: TSESTree.Node): void {
111111
const type = getNodeType(node);
112112

113-
// Conditional is always necessary if it involves `any` or `unknown`
114-
if (isTypeFlagSet(type, TypeFlags.Any | TypeFlags.Unknown)) {
113+
// Conditional is always necessary if it involves:
114+
// `any` or `unknown` or a naked type parameter
115+
if (
116+
unionTypeParts(type).some(part =>
117+
isTypeFlagSet(
118+
part,
119+
TypeFlags.Any | TypeFlags.Unknown | ts.TypeFlags.TypeParameter,
120+
),
121+
)
122+
) {
115123
return;
116124
}
117125
if (isTypeFlagSet(type, TypeFlags.Never)) {

packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ const t1 = (b1 && b2) ? 'yes' : 'no'`,
7272
function test<T extends string>(t: T) {
7373
return t ? 'yes' : 'no'
7474
}`,
75+
`
76+
// Naked type param
77+
function test<T>(t: T) {
78+
return t ? 'yes' : 'no'
79+
}`,
80+
`
81+
// Naked type param in union
82+
function test<T>(t: T | []) {
83+
return t ? 'yes' : 'no'
84+
}`,
7585

7686
// Boolean expressions
7787
`

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy