org.sonar.l10n.javascript.rules.javascript.S4622.html Maven / Gradle / Ivy
Why is this an issue?
Union types represent a value that can be one of the several types. When a union type is used for a function parameter and it is accepting too many
types, it may indicate the function is having too many responsibilities. Sometimes it’s worth creating a type alias for this union type. In all cases,
the code should be reviewed and refactored to make it more maintainable.
Noncompliant code example
With the default threshold of 3:
let x: MyType1 | MyType2 | MyType3 | MyType4; // Noncompliant
function foo(p1: string, p2: MyType1 | MyType2 | MyType3 | MyType4) { // Noncompliant
// ...
}
Compliant solution
type MyUnionType = MyType1 | MyType2 | MyType3 | MyType4; // Compliant, "type" statements are ignored
let x: MyUnionType;
function foo(value: string, padding: MyUnionType) {
// ...
}
Exceptions
This rule ignores union types part of type
statement:
type MyUnionType = MyType1 | MyType2 | MyType3 | MyType4;
It also ignores union types used with TypeScript utility types:
type PickedType = Pick<SomeType, 'foo' | 'bar' | 'baz' | 'qux'>;
Resources
Documentation
- TypeScript Documentation - Utility Types
© 2015 - 2024 Weber Informatics LLC | Privacy Policy