Skip to content

Commit 1d4a70d

Browse files
committed
update docusaurus
1 parent cba2a4a commit 1d4a70d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

website/src/pages/index.mdx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,14 @@ Pascal case
945945
946946
#### Generics
947947
948-
A generic variable must start with the capital letter T followed by a descriptive name `TRequest`, `TFooBar`.
948+
A generic type parameter must start with the capital letter T followed by a descriptive name `TRequest`, `TFooBar`.
949949
950-
Creating more complex types often include generics, which can make them hard to read and understand, that's why we try to put best effort when naming them.
951-
Naming generics using popular convention with one letter `T`, `K` etc. is not allowed, the more variables we introduce, the easier it is to mistake them.
950+
Key reasons and benefits:
951+
952+
- Complex types often involve generics, where clear naming improves readability and maintainability.
953+
- Single letter generics like `T`, `K`, `U` are disallowed, the more parameters we introduce, the easier it is to mistake them.
954+
- Prefixing with `T` makes it immediately obvious that it's a generic type parameter, not a regular type.
955+
- A common scenario is when a generic parameter shadows an existing type due to having the same name e.g. `<Request extends Request>`
952956
953957
<Rule href="https://typescript-eslint.io/rules/naming-convention">
954958
{`'@typescript-eslint/naming-convention': [
@@ -963,17 +967,23 @@ Naming generics using popular convention with one letter `T`, `K` etc. is not al
963967
</Rule>
964968
965969
```ts
966-
// ❌ Avoid naming generics with one letter
970+
// ❌ Avoid naming generic parameters with one letter
967971
const createPair = <T, K extends string>(first: T, second: K): [T, K] => {
968972
return [first, second];
969973
};
970974
const pair = createPair(1, 'a');
971975

972-
//Name starts with the capital letter T followed by a descriptive name
976+
//Use descriptive names starting with capital T
973977
const createPair = <TFirst, TSecond extends string>(first: TFirst, second: TSecond): [TFirst, TSecond] => {
974978
return [first, second];
975979
};
976980
const pair = createPair(1, 'a');
981+
982+
// ❌ Avoid naming generic parameters without a prefix - which 'Request' is which?
983+
const handle = <Request extends Request>(req: Request): void => {...
984+
985+
// ✅ Prefix generic parameter with capital T
986+
const handle = <TRequest extends Request>(req: TRequest): void => {...
977987
```
978988
979989
#### Abbreviations & Acronyms

0 commit comments

Comments
 (0)