Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
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 havesearched for related issues and found none that matched my issue.
- I haveread the FAQ and my problem is not listed.
Playground Link
Repro Code
consts="螺𩷶屬";console.log(JSON.stringify([...s]));// Correct: ["螺","𩷶","屬"]// But no-misused-spread complains there, and suggests:console.log(JSON.stringify(s.split("")));// Incorrect: ["螺","\ud867","\uddf6","屬"]
ESLint Config
module.exports={"rules":{"@typescript-eslint/no-misused-spread":"error"}}
tsconfig
{"compilerOptions": {"strictNullChecks":true }}
Expected Result
In JavaScript, string spreading is the correct way to split a string into Unicode code points. It should not be an error.
Actual Result
error Using the spread operator on a string can cause unexpected behavior. Prefer `.split('')` instead. @typescript-eslint-no-misused-spread
But.split('')
splits a string into UTF-16 code units, not Unicode code points. For characters outside the basic multilingual plane, this produces invalid unpaired surrogates.
Additional Info
This rule was introduced with a justification linking to the blog post “Strings Shouldn’t Be Iterable By Default”. But this post is about how to design better languages in the future, not how to use the languages we already have.
In the JavaScript language we have,[...s]
is the correct way to split a string into Unicode code points, and.split('')
does not do that correctly. The suggestion is harmful and will result in the introduction of subtle internationalization bugs triggered with less common languages (and emoji).
There isn’t another good way to correctly split a string into Unicode code points, so this rule should just be disabled for strings.