Skip to content

fix(eslint-plugin): [no-unnecessary-cond] fix naked type param #1207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2019
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