noUnnecessaryConditions
Este conteúdo não está disponível em sua língua ainda.
Summary
Section titled “Summary”- Rule available since:
vv2.1.4
- Diagnostic Category:
lint/nursery/noUnnecessaryConditions
- This rule doesn’t have a fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
- Sources:
Description
Section titled “Description”Disallow unnecessary type-based conditions that can be statically determined as redundant.
This rule detects if expressions inside conditions are statically inferrable and yield falsy or truthy values that don’t change during the life cycle of the program.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function head<T>(items: T[]) { if (items) { // This check is unnecessary return items[0].toUpperCase(); }}
function foo(arg: 'bar' | 'baz') { if (arg) { // This check is unnecessary }}
function bar(arg: string) { return arg?.length; // ?. is unnecessary}
function head<T>(items: T[] | null) { if (items) { // This check is necessary return items[0].toUpperCase(); }}
function foo(arg: 'bar' | 'baz' | null) { if (arg) { // This check is necessary }}
function bar(arg: string | undefined) { return arg?.length; // ?. is necessary}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUnnecessaryConditions": "error" } } }}