52 lines
1.2 KiB
Plaintext
52 lines
1.2 KiB
Plaintext
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:
|
|
|
|
[source,javascript]
|
|
----
|
|
let x: MyType1 | MyType2 | MyType3 | MyType4; // Noncompliant
|
|
|
|
function foo(p1: string, p2: MyType1 | MyType2 | MyType3 | MyType4) { // Noncompliant
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,javascript]
|
|
----
|
|
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;
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::parameters.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|