10000 Bug: [no-empty-object-type] false positive/incorrect fixes for default type parameter · Issue #9761 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [no-empty-object-type] false positive/incorrect fixes for default type parameter #9761

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
printfn opened this issue Aug 9, 2024 · 8 comments
Closed
4 tasks done
Labels
bug Something isn't working 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 working as intended Issues that are closed as they are working as intended

Comments

@printfn
Copy link
Contributor
printfn commented Aug 9, 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=.tsx&code=KYDwDg9gTgLgBASwHY2FAZgQwMbDgSQB4AFOUVJAEwGc4AlYbaSw6mKZAcwBo42OknAHxwAvHADeAXxESAsACg4cMJiiYAttQBccYgG5FUoA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge3oFsbDaOAIwBWiMnSKl0URNGgdokcGAC%2BIFUA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A&tokens=false

Repro Code

export interface I<P extends Record<string, string> = {}> {
  params: P;
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-empty-object-type": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

I'm not sure this should even produce an error. Neither of the suggested fixes works.

Actual Result

The {} ("empty object") type allows any non-nullish value, including literals like 0 and "".

  • If that's what you want, disable this lint rule with an inline comment or configure the 'allowObjectTypes' rule option.
  • If you want a type meaning "any object", you probably want object instead.
  • If you want a type meaning "any value", you probably want unknown instead. 5:55 - 5:57

Replace {} with object.
Replace {} with unknown.

Additional Info

No response

@printfn printfn 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 Aug 9, 2024
@Josh-Cena
Copy link
Member
Josh-Cena commented Aug 9, 2024

The suggestions are there for common cases; neither of them are equivalent to {} and we don't guarantee that your code continues to work. In this case, the report is expected because it may result in unexpected behaviors:

export interface I<P extends Record<string, string> = {}> {
  params: P;
}

const x: I = { params: 1 }; // Allowed

You can use P extends Record<string, string> = Record<string, string> as the default, or P extends Record<string, string> = Record<string, never> if you want the default to not contain any properties, or even just disable the rule here, if you know what you are writing is intended.

@bradzacher
Copy link
Member

Yeah this reporting is entirely correct and using {} here is unsafe as Josh mentions.
The suggestions are exactly that - suggestions. They aren't automatically applied and must be manually evaluated and manually applied.

In this case Josh's suggestion is correct - you have a generic constraint, so use the constraint as the default. Though you probably want something like Record<string, never> which would force it to be an empty object and accept { params: {} } and reject { params: 1 } and { params: { prop: 1 } }.


@typescript-eslint/triage-team -- what do we think about adding a suggestion here:
If we detect that the {} is used as a generic default AND the generic has a constraint, then we additionally suggest the constraint as a special case suggestion

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for team members to take a look labels Aug 9, 2024
@Josh-Cena
Copy link
Member

Hmmm, negative—I don't think that is going to help in most cases and really complicates the code.

@kirkwaiblinger
Copy link
Member

-1 from me, too, I'd rather avoid blessing Record<string, never> with a typescript-eslint suggestion (see also #8977 (comment) and microsoft/TypeScript#57735 (comment))

@bradzacher
Copy link
Member
bradzacher commented Aug 9, 2024

I meant specifically if the code is of the form <T extends U = {}>
Then we would additionally suggest replacing {} with U (i.e. <T extends U = U>)
So in this case the additional suggestion would suggest Record<string, string> (i.e. <P extends Record<string, string> = Record<string, string>>)

@Josh-Cena
Copy link
Member

Yeah, I meant—this is not always, if ever, going to be what the user intends, and only solves a handful of cases out-of-the-box.

@kirkwaiblinger
AB0F Copy link
Member

Oh, I misunderstood, thanks for clarifying. +/- 0 from me on this 🤷‍♂️

@bradzacher
Copy link
Member

Okay then it seems like we're in agreement - this is working as intended and no changes shall be made.

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Aug 9, 2024
@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 Aug 17, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working 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 working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

4 participants
0