Skip to content

charAtComparisons

Reports comparing charAt() results with strings longer than one character.

✅ This rule is included in the ts logical and logicalStrict presets.

The charAt method returns a string of exactly one character. If the return value is compared with a string of length greater than one, the comparison will always evaluate to the same result. Equality comparisons (===, ==) will always be false, and inequality comparisons (!==, !=) will always be true.

const text: string
text
.
String.charAt(pos: number): string

Returns the character at the specified index.

@parampos The zero-based index of the desired character.

charAt
(0) === "ab";
"abc" ===
const text: string
text
.
String.charAt(pos: number): string

Returns the character at the specified index.

@parampos The zero-based index of the desired character.

charAt
(0);
const text: string
text
.
String.charAt(pos: number): string

Returns the character at the specified index.

@parampos The zero-based index of the desired character.

charAt
(0) !== "hello";

This rule is not configurable.

This rule catches a specific programming error with no legitimate use cases. There is no reason to disable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.