33 lines
1.2 KiB
Plaintext
33 lines
1.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
TypeScript provides the `type` mechanism to create a type alias, that is, a new name to refer to an existing type. It is a nice feature to improve code readability and can be used as documentation. However, aliasing a primitive type, another alias or everyday types like `any` or `unknown` is useless and goes against the idea of readable code. As a result, a reader needs to go through the mental exercise of remembering the underlying type behind the alias and loses track of the code's primary purpose.
|
|
|
|
Common types come with relevant names and should be used as they are.
|
|
|
|
== How to fix it
|
|
|
|
The type alias should be removed, and all the occurrences of the alias should be replaced with the type it referred to.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,typescript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
type MyString = string;
|
|
type MyBoolean = boolean;
|
|
|
|
function isPalindrom(s: MyString): MyBoolean {
|
|
return s === s.split('').reverse().join('');
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,typescript,diff-id=1,diff-type=compliant]
|
|
----
|
|
function isPalindrom(s: string): boolean {
|
|
return s === s.split('').reverse().join('');
|
|
}
|
|
----
|