-
Notifications
You must be signed in to change notification settings - Fork 229
Description
Problem:
This assertion is unnecessary since it does not change the type of the expression
Why is this an issue?
In TypeScript, casts and non-null assertions are two mechanisms used to inform the TypeScript compiler about the intended types of variables, expressions, or values in the code. They are used to help the compiler understand the types more accurately and to handle certain situations where type inference might not be sufficient:
- A type assertion tells the compiler to treat the value as the specified type, even if the compiler’s type inference suggests a different type.
- A non-null assertion is a way to tell the TypeScript compiler explicitly that you are certain that a variable will not be null or undefined, and you want to access its properties or methods without any null checks.
However, a redundant cast occurs when you perform a type assertion that is not needed because the compiler already knows the type based on the context or explicit type declarations. Similarly, a redundant non-null assertion occurs when you use the non-null assertion operator ! to assert that a variable is not null or undefined, but the compiler already knows that based on the context.
Both redundant casts and redundant non-null assertions should be avoided in TypeScript code, as they add unnecessary noise, clutter the code, and lead to confusion.
function getName(x?: string | Person) {
if (x) {
console.log("Getting name for " + x!); // Noncompliant: 'x' is known to be defined here
if (typeof x === "string") {
return (x as string); // Noncompliant: 'x' is known to be a string here
} else {
return (x as Person).name; // Noncompliant: 'x' is defined and not a string, thus a 'Person' here
}
}
return "NoName";
}
Remove all the redundant casts and non-null assertions based on the contextual typing information, as inferred by the TypeScript compiler.
function getName(x?: string | Person) {
if (x) {
console.log("Getting name for " + x);
if (typeof x === "string") {
return x;
} else {
return x.name;
}
}
return "NoName";
}
Problem locations:
packages/ketcher-react/src/script/ui/state/modal/sdata.ts
packages/ketcher-react/src/script/ui/component/actionmenu.tsx
Metadata
Metadata
Assignees
Labels
Type
Projects
Status