-
- Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Labels
Description
Is there an existing issue for this?
- I have searched the existing issues and my issue is unique
- My issue appears in the command-line and not only in the text editor
Description Overview
The forbidDefaultForRequired option in the react/require-default-props rule does not work as expected
Using the react/require-default-props rule together with the forbidDefaultForRequired option set to true does not throw an error or warning when a prop that is required is assigned a default value.
Eslint rule
{ "rules": { "react/require-default-props": ["error", { "forbidDefaultForRequired": true, "functions": "defaultArguments" }], } }Example code:
function MyStatelessComponent({ foo = 'foo', bar }) { return <div>{foo}{bar}</div>; } MyStatelessComponent.propTypes = { foo: PropTypes.string.isRequired, bar: PropTypes.string };type MyStatelessComponentProps = { foo: string; bar?: string } function MyStatelessComponent({ foo = 'foo', bar }: MyStatelessComponentProps) { return <div>{foo}{bar}</div>; } MyStatelessComponent.propTypes = { foo: PropTypes.string.isRequired, bar: PropTypes.string };In the previous examples the foo prop is required, but it is being assigned a default value, with the forbidDefaultForRequired option set totrue, without throwing errors or warnings.
Expected Behavior
The expected behavior is that if a prop is mandatory and a default value is assigned, it should throw an error or warning depending on the rule configuration.
eslint-plugin-react version
7.32.2
eslint version
8
node version
18