8000 Bug: [no-unnecessary-condition] does not report unnecessary default value during object destructuring · Issue #10082 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [no-unnecessary-condition] does not report unnecessary default value during object destructuring #10082

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

Closed
4 tasks done
TkDodo opened this issue Oct 1, 2024 · 6 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look

Comments

@TkDodo
Copy link
TkDodo commented Oct 1, 2024

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.5.2&fileType=.ts&code=GYVwdgxgLglg9mABAYTgWwA4IKZigCgG9Fg45EBeRARkQF8AuRY0uJsENAI2wCd6AlMwBQiRL2xQQvJKwDcwusKA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function Component({ foo = 1 }: { foo: number }) {
  return foo;
}

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I would expect a lint error here form no-unnecessary-condition because the default value assignment can never be reached, given that foo is mandatory in the object

Actual Result

no error shown

Additional Info

The rule correctly reports an error when nullish coalesce is used instead:

function Component2(props: { foo: number }) {
  const foo = props ?? 1

  return foo
}

Unnecessary conditional, expected left-hand side of ?? operator to be possibly null or undefined.

@TkDodo TkDodo added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Oct 1, 2024
@bradzacher
Copy link
Member
bradzacher commented Oct 1, 2024
8000

I would argue that this doesn't really fit into this existing rule. Josh is correct that there is an implicit condition hidden in this operation - it desugars to arg = arg === undefined ? default : arg - but syntactically it is an assignment.

Personally I think that a better fit would be a type-aware extension rule for no-useless-assignment.

But we don't love extension rules - so perhaps instead it would be better to live as it's own rule that we can add to the recommended set?

@TkDodo
Copy link
Author
TkDodo commented Oct 1, 2024

yeah I don’t know enough about how you’d like to structure the rules; I’m fine with either 👍

@bradzacher
Copy link
Member

Also worth noting that we could expand this to all destructuring and not just argument destructuring.

Eg the rule could also flag:

const {x = 1} = {x: 1};
const [y = 1] = [1];

@ronami
Copy link
Member
ronami commented Oct 1, 2024

+1 this would be useful though I think it's a duplicate of #6106.

It's also relevant for default parameters, not necessarily destructuring:

[1, 2, 3].map((a = 42) => a + 1)

@TkDodo
Copy link
Author
TkDodo commented Oct 1, 2024

+1 this would be useful though I think it's a duplicate of #6106.

indeed; feel free to close as duplicate

@kirkwaiblinger kirkwaiblinger closed this as not planned Won't fix, can't repro, duplicate, stale Oct 1, 2024
@kirkwaiblinger kirkwaiblinger added the duplicate This issue or pull request already exists label Oct 1, 2024
@JoshuaKGoldberg
Copy link
Member

D'oh, and the last commenter in 6106 was me, 22 months ago. Sorry for the switch-up TkDodo!

@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Oct 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working duplicate This issue or pull request already exists locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look
Projects
None yet
Development

No branches or pull requests

5 participants
0