TypeScript S6606 triggering with String types

  • What language is this for? Typescript
  • Which rule? S6606
  • Why do you believe it’s a false-positive/false-negative?
    The suggested fix is to simply replace the logical OR with the nullish coalescing operator, but for string types, there’s side effects when you’re depending on the fallback behavior when there’s an empty string present. This seems to be present when there’s a string type that may also be null or undefined (ex. string | null)
  • Are you using
    • SonarQube Cloud? Yes
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
interface DefaultSession {
  user?: {
    name?: string | null;
    email?: string | null;
    image?: string | null;
  };
  expires: string;
}

function displayUserInfo(session: DefaultSession): string {
  return session.user?.name || session.user?.email || "User";
}

const session: DefaultSession = {
  user: {
    name: "",
    email: "user@example.com",
  },
  expires: "2025-05-15T00:00:00Z",
};

console.log(displayUserInfo(session)); 

Here, the desired output would be user@example.com; however, if it were changed to session.user?.name ?? session.user?.email ?? "User", then the output would just be an empty string.

Hello @_aura,

we are aware of this rule raising FP against strings. We have a ticket opened to fix this. Should be fixed in the next couple of months.

Thanks for reporting it!

Cheers,
Victor