Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [no-unnecessary-condition] don't flag optional chaining for union types with an unconstrained type parameters#10602

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
28 changes: 15 additions & 13 deletionspackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -304,6 +304,19 @@ export default createRule<Options, MessageId>({
);
}

// Conditional is always necessary if it involves:
// `any` or `unknown` or a naked type variable
function isConditionalAlwaysNecessary(type: ts.Type): boolean {
return tsutils
.unionTypeParts(type)
.some(
part =>
isTypeAnyType(part) ||
isTypeUnknownType(part) ||
isTypeFlagSet(part, ts.TypeFlags.TypeVariable),
);
}

function isNullableMemberExpression(
node: TSESTree.MemberExpression,
): boolean {
Expand DownExpand Up@@ -370,18 +383,7 @@ export default createRule<Options, MessageId>({

const type = getConstrainedTypeAtLocation(services, expression);

// Conditional is always necessary if it involves:
// `any` or `unknown` or a naked type variable
if (
tsutils
.unionTypeParts(type)
.some(
part =>
isTypeAnyType(part) ||
isTypeUnknownType(part) ||
isTypeFlagSet(part, ts.TypeFlags.TypeVariable),
)
) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

[Praise] Heh, yeah, this was getting a little hard to read inline even before...

ronami reacted with heart emoji
if (isConditionalAlwaysNecessary(type)) {
return;
}
let messageId: MessageId | null = null;
Expand DownExpand Up@@ -813,7 +815,7 @@ export default createRule<Options, MessageId>({
: true;

return (
isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown) ||
isConditionalAlwaysNecessary(type) ||
(isOwnNullable && isNullableType(type))
);
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -143,6 +143,24 @@ function test<T>(t: T | []) {
return t ? 'yes' : 'no';
}
`,
`
function test<T>(arg: T, key: keyof T) {
if (arg[key]?.toString()) {
}
}
`,
`
function test<T>(arg: T, key: keyof T) {
if (arg?.toString()) {
}
}
`,
`
function test<T>(arg: T | { value: string }) {
if (arg?.value) {
}
}
`,

// Boolean expressions
`
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp