Enforce using String#startsWith and String#endsWith over other equivalent methods of checking substrings.
// code-linter.json5
{
"rules": {
"@typescript-eslint/prefer-string-starts-ends-with": "error"
}
} declare const foo: string;
// starts with
foo.startsWith('bar');
// ends with
foo.endsWith('bar'); declare const foo: string;
declare const index: number;
// starts with
foo[index] === 'b';
foo.charAt(index) === 'b';
foo.indexOf('bar') === index;
foo.slice(index) === 'bar';
foo.substring(index) === 'bar';
foo.match(/^bar/) !== null;
/^bar/.test(foo);
// ends with
foo[foo.length - index] === 'b';
foo.charAt(foo.length - index) === 'b';
foo.lastIndexOf('bar') === foo.length - index;
foo.slice(-index) === 'bar';
foo.substring(foo.length - index) === 'bar';
foo.match(/bar$/) !== null;
/bar$/.test(foo); plugin:@typescript-eslint/all
For details about how to configure Code Linter check rules, see Code Linter.