Victor 38626b0c08
Modify rule S4335: Adapt to LaYC (#2693)
Co-authored-by: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com>
2023-07-28 11:10:41 +02:00

64 lines
2.8 KiB
Plaintext

== Why is this an issue?
In TypeScript, intersections are used to combine multiple types into a single one. An intersection type is represented using the ampersand symbol ``++&++``. It allows you to combine multiple types into a single type that includes all the properties and methods from each type, thus creating more flexible and powerful type definitions.
However, some of the basic types of TypeScript should not be used with intersections:
* The `never` type represents the type of values that never occur. Intersecting any type with `never` will always result in type `never`.
* The `any` type allows to opt-out of type checking during compilation. Expressions of type `any` allow you to access arbitrary properties, even ones that don't exist. `any` comes at the cost of losing type safety, which is one of the main motivations for using TypeScript. Avoid using `any` when not necessary. Intersecting any type with `any` will always result in type `any`.
* The `undefined` and `null` types are the types for their respective value. Intersecting any type with them will always result in type `never`.
* The `void` type implies the absence of a type. Intersecting any type with `void` will always result in type `never`.
Additionally, an intersection with a type without members (for example, ``++{}++``) doesn't change the resulting type, is redundant, and can be safely removed from the intersection.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
type Foo = T & null; // Noncompliant: 'Foo' is always 'never'
type Bar = T & any; // Noncompliant: 'Bar' is always 'any'
type Baz = T & U & {}; // Noncompliant: '{}' has no members and is redundant
----
Use consistent types that accurately reflect the domain of values of the defined data type.
[source,javascript,diff-id=1,diff-type=compliant]
----
type Foo = T | null;
type Bar = T & U;
type Baz = T & U;
----
== Resources
=== Documentation
* https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types[TypeScript - Intersection Types]
* https://www.typescriptlang.org/docs/handbook/basic-types.html#never[TypeScript - `never`]
* https://www.typescriptlang.org/docs/handbook/basic-types.html#any[TypeScript - `any`]
* https://www.typescriptlang.org/docs/handbook/basic-types.html#null-and-undefined[TypeScript - `null` and `undefined`]
* https://www.typescriptlang.org/docs/handbook/basic-types.html#void[TypeScript - `void`]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Remove this type without members or change this type intersection.
* Simplify this intersection as it always has type ["any" | "never"].
=== Highlighting
type without members or full intersection if it has ``++any++`` or ``++never++``
endif::env-github,rspecator-view[]