Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-cond] fix naked type param (#1207)
Browse files Browse the repository at this point in the history
  • Loading branch information
Retsam authored and bradzacher committed Nov 15, 2019
1 parent 259ff20 commit 4fac6c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -110,8 +110,16 @@ export default createRule<Options, MessageId>({
function checkNode(node: TSESTree.Node): void {
const type = getNodeType(node);

// Conditional is always necessary if it involves `any` or `unknown`
if (isTypeFlagSet(type, TypeFlags.Any | TypeFlags.Unknown)) {
// Conditional is always necessary if it involves:
// `any` or `unknown` or a naked type parameter
if (
unionTypeParts(type).some(part =>
isTypeFlagSet(
part,
TypeFlags.Any | TypeFlags.Unknown | ts.TypeFlags.TypeParameter,
),
)
) {
return;
}
if (isTypeFlagSet(type, TypeFlags.Never)) {
Expand Down
Expand Up @@ -72,6 +72,16 @@ const t1 = (b1 && b2) ? 'yes' : 'no'`,
function test<T extends string>(t: T) {
return t ? 'yes' : 'no'
}`,
`
// Naked type param
function test<T>(t: T) {
return t ? 'yes' : 'no'
}`,
`
// Naked type param in union
function test<T>(t: T | []) {
return t ? 'yes' : 'no'
}`,

// Boolean expressions
`
Expand Down

0 comments on commit 4fac6c5

Please sign in to comment.