Why this matters
Unsafe type assertions do not perform runtime checks, leading to potential runtime errors if the assertion is incorrect. Use proper type checks instead.
Detect cases of unsafe type assertions. These do not perform runtime checks and can lead to unexpected runtime errors. Recommend using proper type guards instead.
Unsafe type assertions do not perform runtime checks, leading to potential runtime errors if the assertion is incorrect. Use proper type checks instead.
Side-by-side examples engineers can pattern-match during review.
const element = document.getElementById("myId") as HTMLInputElement;
element.value = "Hello"; // Unsafe if the element is not an inputconst element = document.getElementById("myId");
if (element instanceof HTMLInputElement) {
element.value = "Hello";
}
const element = document.getElementById("myId") as HTMLInputElement;
element.value = "Hello"; // Unsafe if the element is not an inputconst element = document.getElementById("myId");
if (element instanceof HTMLInputElement) {
element.value = "Hello";
}
From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.