Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
Metadata
ID:typescript-best-practices/default-param-last
Language: TypeScript
Severity: Warning
Category: Best Practices
Description
This rule encourages the practice of defining default parameters after the normal parameters in a function declaration. This is to ensure that the function behaves as expected when it is called with fewer arguments.
Default parameters are used to initialize formal parameters with default values. They are useful when an argument is not provided in the function or if it is undefined. If the function is called with fewer arguments than the declared parameters, the normal parameter receives undefined, while the default parameter is initialized with the provided value.
To avoid this, ensure that normal parameters are always defined before default parameters. This ensures that the function behaves as expected and doesn’t cause any unexpected results.
Non-Compliant Code Examples
functionfoo(a=false,b){}foo(undefined,"b")
Compliant Code Examples
functionfoo(a,b=false){}foo("a")
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- typescript-best-practices # Rules to enforce TypeScript best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines