False positive in S4782 for imported type

  • What language is this for? TypeScript
  • Which rule? S4782
  • Why do you believe it’s a false-positive/false-negative? The behavior directly contradicts documentation and following the recommendation breaks typechecking.
  • Are you using
    • SonarQube for IDE - for VSCode v5.6.0, does not appear to be in connected mode.
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
import {
	TextField,
} from '@mui/material';
import type {
	TextFieldProps,
} from '@mui/material';

export function ComponentUser() {
	return (
		<WrappedTextField
			spanClasses='w-full'
		/>
	);
}

export function WrappedTextField(props: Readonly<{
	spanClasses: string,
	//Dropping the ? in the next line causes an error above,
	//because the calling context does not pass a size prop.
	//The last line of the issue details for typescript:S4782 states:
	//"The rule also suppresses issues when undefined is only reachable
	//through a type defined in an external library
	//(for example, React.ReactNode).
	//In that case the user cannot modify the library’s type declaration,
	//so flagging the property would produce a false positive."
	//That is not true (TextFieldProps is imported from an external library),
	//but its presence in the documentation
	//signaling the intended correct behavior
	//clearly signals that the false positive here is a bug in SonarQube.
	//Note: You may need to touch tsconfig.js to observe the issue.
	size?: TextFieldProps['size'],
}>) {
	return (
		<span
			className={props.spanClasses}
		>
		<TextField
			size={props.size}
		/>
		</span>
	);
};

Note: Using plain import instead of type-only import doesn’t help.

Hi @unblocker,

Thank you for bringing this to our attention. We’ve confirmed that S4782 incorrectly raises an issue in this situation.

The fix is tracked in JS-2192.

We appreciate the detailed report. It’s very helpful!

François