Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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