From 4fac6c5c40b1ff171db7ff6913534d307ac5a169 Mon Sep 17 00:00:00 2001 From: Retsam Date: Thu, 14 Nov 2019 20:25:27 -0500 Subject: [PATCH] fix(eslint-plugin): [no-unnecessary-cond] fix naked type param (#1207) --- .../src/rules/no-unnecessary-condition.ts | 12 ++++++++++-- .../tests/rules/no-unnecessary-condition.test.ts | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 49c8f13401c..8e75f7adb3f 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -110,8 +110,16 @@ export default createRule({ 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)) { diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index f298495a26f..c5d90fd01ef 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -72,6 +72,16 @@ const t1 = (b1 && b2) ? 'yes' : 'no'`, function test(t: T) { return t ? 'yes' : 'no' }`, + ` +// Naked type param +function test(t: T) { + return t ? 'yes' : 'no' +}`, + ` +// Naked type param in union +function test(t: T | []) { + return t ? 'yes' : 'no' +}`, // Boolean expressions `