stringSliceMethods
Reports usage of
substringinstead of slice for string operations.
✅ This rule is included in the ts logical presets.
JavaScript provides three methods for extracting substrings: slice(), substr(), and substring().
This rule encourages consistent use of slice() because:
substr()is deprecated and removed from the ECMAScript specificationsubstring()has confusing behavior: it auto-swaps arguments when start > end, and treats negative indices as 0slice()has consistent, predictable behavior that matchesArray.prototype.slice()
Examples
Section titled “Examples”substr to slice
Section titled “substr to slice”44AEcosubstring to slice
Section titled “substring to slice”44AEcoNegative indices
Section titled “Negative indices”44AEcoOptions
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you’re maintaining legacy code that heavily uses substr() or substring() and refactoring would be too costly, you may need to disable this rule.
Note that substr() is deprecated and may be removed from JavaScript engines in the future.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noSubstr - ESLint:
unicorn/prefer-string-slice - Oxlint:
unicorn/prefer-string-slice
