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”const result = text.substr(1, 5);const result = text.slice(1, 6);substring to slice
Section titled “substring to slice”const result = text.substring(1, 5);const result = text.slice(1, 5);Negative indices
Section titled “Negative indices”const last = text.substring(text.length - 3);const last = text.slice(-3);Options
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
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.