8000 Bug: [consistent-generic-constructors] improper fix into `new Uint8Array<ArrayBufferLike>()` · Issue #10445 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [consistent-generic-constructors] improper fix into new Uint8Array<ArrayBufferLike>() #10445

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

Open
4 tasks done
yoursunny opened this issue Dec 3, 2024 · 7 comments · May be fixed by #10477
Open
4 tasks done

Bug: [consistent-generic-constructors] improper fix into new Uint8Array<ArrayBufferLike>() #10445

yoursunny opened this issue Dec 3, 2024 · 7 comments · May be fixed by #10477
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@yoursunny
Copy link

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.7.2&fileType=.ts&code=DYUwLgBAhgXBCqBLAdmAHAQQE5agTwB5tc8AhAVwDNKQsAZRAaxAD4IBeCZEAdwRXTF8ACgCUAbgBQANwD2iACbQpQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6MgeyeUuX0Ra0A5gMRUytTt3xwy%2BDtFQZIY6AsjgwAXxBagA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkqyARhWANqTokBMADJwMyQAugBpwNSPFgATAK4Zm7Lr06RRNKCTyZk0PADk5qAMIALdNADWZSlpno14vAENMAc3R4FHHv0hiAviD%2BQA&tokens=false

Repro Code

let a: Uint8Array<ArrayBufferLike> = new Uint8Array();
void a;

ESLint Config

{
  "rules": {
    "@typescript-eslint/consistent-generic-constructors": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "lib": ["es2023"],
    "module": "es2022",
    "strictNullChecks": true,
    "target": "es2023"
  }
}

Expected Result

Either don't report the error, or don't generate invalid code upon fix.

Actual Result

Rule reports error:

The generic type arguments should be specified as part of the constructor type arguments. 1:5 - 1:54

If I tap Fix, the code becomes:

let a = new Uint8Array<ArrayBufferLike>();
void a;

And then TypeScript reports error:

2554: Expected 1-3 arguments, but got 0. 1:9 - 1:42

Additional Info

If I enable two rules consistent-generic-constructors and no-unnecessary-type-arguments, playground, both rules would report errors.
The outcome depends on the order of fix.

  • If consistent-generic-constructors is fixed first, the above problem occurs.
  • If no-unnecessary-type-arguments is fixed first, the resulting code is well-formed.
@yoursunny yoursunny 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 Dec 3, 2024
@yoursunny yoursunny changed the title Bug: [consistent-generic-constructors] <short description of the issue> Bug: [consistent-generic-constructors] improper fix into new Uint8Array<ArrayBufferLike>() Dec 3, 2024
@Josh-Cena
Copy link
Member

This rule is not type-aware and cannot detect that the type Uint8Array has a different type signature from the value Uint8Array, and I don't think it's a good idea to introduce extra logic in any way to "smartly" solve this case. In the vast majority of cases, let a: Uint8Array<ArrayBufferLike> = new Uint8Array(); and let a = new Uint8Array<ArrayBufferLike>(); should be equivalent code, but here actually Uint8Array() is not a generic constructor (its overloads are), so the rule isn't even applicable.

What do others say? I don't think the TS lib def is wrong or awkward, so maybe we should add an option to ignore selected constructors?

@kirkwaiblinger
Copy link
Member
kirkwaiblinger commented Dec 3, 2024

Noting that this is a new case with TS 5.7, since, previously, one would have gotten the error Type 'Uint8Array' is not generic.. (see also TS 5.7 releaes notes)

I'm wondering if the Uint8Array constructor should be changed upstream to take a type parameter?

@Josh-Cena
Copy link
Member

Uint8Array has overloaded. The form that takes no arguments is just new () => Uint8Array<ArrayBuffer>, which is correct in itself. You could change it to new <T extends ArrayBufferLike = ArrayBuffer> () => Uint8Array<T> but that's extra complexity with no benefits for them.

@kirkwaiblinger
Copy link
Member

Yeah so I guess the rule, without type information, is only really expected to work on non-overloaded class constructors. Which is most anything user-defined, and also probably a majority of the built-in types.

My intuition is that an ignore list makes sense, though UInt8Array and friends should probably be in it by default (or be hard-coded and concatenated). Maybe we hard-code the typed arrays, and put general ignore list up to "evaluating community engagement"?

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Dec 7, 2024
@JoshuaKGoldberg
Copy link
Member

Accepting PRs for specifically the built-in array-likes.

@RoenLie
Copy link
RoenLie commented Jan 27, 2025

Same goes for Proxy.

Image

Copy link

Uh oh! @RoenLie, the image you shared is missing helpful alt text. Check #10445 (comment).

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
5 participants
0