Skip to content
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

Enhancement: [no-misused-spread] Option to allow string literals #10954

Open
4 tasks done
syimyuzya opened this issue Mar 15, 2025 · 2 comments
Open
4 tasks done

Enhancement: [no-misused-spread] Option to allow string literals #10954

syimyuzya opened this issue Mar 15, 2025 · 2 comments
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@syimyuzya
Copy link

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-misused-spread/

Description

The allow option of this rule can be configured to allow spreading strings with { from: 'lib', name: 'string' }, but this does not cover string literals, and there is no way to specify things like "any string literal" using this option.

I propose that we add an option like allowStringLiterals in addition to allow (or make it possible to specify "any string literal" with TypeOrValueSpecifier), which allows spreading of just string literals.

Fail

let myStr: string = "String literals in this project is known to not contain grapheme clusters.";

[...myStr]; // not a string literal type, thus not covered by this option

Pass

[..."String literals in this project is known to not contain grapheme clusters."];

const myStr = "a string const";
[...myStr];

Additional Info

No response

@syimyuzya syimyuzya added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 15, 2025
@bradzacher
Copy link
Member

I'm not quite sure I understand - why should string literal typed values be exempted and special case? You did not explain the motivation for your request.

Why should such an option exist?

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Mar 15, 2025
@syimyuzya
Copy link
Author

I'm not quite sure I understand - why should string literal typed values be exempted and special case? You did not explain the motivation for your request.

Why should such an option exist?

Yeah I've also realized that my proposal might not be the ideal way to solve the problem.

The real problem is: There has to be a way to configure this rule to (dis)allow any string (including literals). (even if not in the way I proposed)

Here are the cases listed in the rule's documentation:

  1. Spreading a Promise into an object. You probably meant to await it.
  2. Spreading a function without properties into an object. You probably meant to call it.
  3. Spreading an iterable (Array, Map, etc.) into an object. Iterable objects usually do not have meaningful enumerable properties and you probably meant to spread it into an array instead.
  4. Spreading a string into an array. String enumeration behaviors in JavaScript around encoded characters are often surprising.
  5. Spreading a class into an object. This copies all static own properties of the class, but none of the inheritance chain.
  6. Spreading a class instance into an object. This does not faithfully copy the instance because only its own properties are copied, but the inheritance chain is lost, including all its methods.

Say, if I want to turn on/off this rule just for case 4 (spreading strings), I would naturally expect that this rule can be configured such, which isn't possible now. (same for the other 5 cases too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants