|
| 1 | +--- |
| 2 | +slug: comments |
| 3 | +title: "Creating rich comments" |
| 4 | +authors: [unional] |
| 5 | +tags: [comment, documentation, DX] |
| 6 | +--- |
| 7 | + |
| 8 | +# Comments |
| 9 | + |
| 10 | +You **should** add JSDoc comments to your code. |
| 11 | + |
| 12 | +> Why? |
| 13 | +
|
| 14 | +For a long time, I does not do this. |
| 15 | +My belief was that the code should be self-explanatory. |
| 16 | + |
| 17 | +However, having the comments in the code, |
| 18 | +especially when it is a public API, |
| 19 | +makes it a lot easier for consumer to use the code. |
| 20 | + |
| 21 | +Especially if you can provide examples in the comments. |
| 22 | + |
| 23 | +For example, the following is a comment for the `Equal` type in [type-plus]: |
| 24 | + |
| 25 | +```ts |
| 26 | +/** |
| 27 | + * Checks `A` and `B` are equal. |
| 28 | + * |
| 29 | + * ```ts |
| 30 | + * type R = Equal<1, 1> // true |
| 31 | + * type R = Equal<any, any> // true |
| 32 | + * type R = Equal<boolean, boolean> // true |
| 33 | + * type R = Equal<true, true> // true |
| 34 | + * type R = Equal<[1], [1]> // true |
| 35 | + * |
| 36 | + * type R = Equal<boolean, true> // false |
| 37 | + * type R = Equal<any, 1> // false |
| 38 | + * type R = Equal<[any], [1]> // false |
| 39 | + * type R = Equal<{ a: 1 }, { a: 1; b: 2 }> // false |
| 40 | + * ``` |
| 41 | + */ |
| 42 | +export type Equal<A, B, Then = true, Else = false> = ... |
| 43 | +``` |
| 44 | +
|
| 45 | +--- |
| 46 | +
|
| 47 | +You **should not** include import statements in the comment examples. |
| 48 | +
|
| 49 | +> Why? |
| 50 | +
|
| 51 | +Your code or type can be reused in different context. |
| 52 | +The import statement might not be the same in different contexts. |
| 53 | +
|
| 54 | +For example, your code might be reused in another package. |
| 55 | +So the import statement will provide the wrong information. |
| 56 | +
|
| 57 | +❌ Bad |
| 58 | +
|
| 59 | +```ts |
| 60 | +/** |
| 61 | + * Check if the type `T` is exactly `any`. |
| 62 | + * |
| 63 | + * ```ts |
| 64 | + * import type { AnyType } from 'type-plus' |
| 65 | + * |
| 66 | + * type R = AnyType<any> // any |
| 67 | + * |
| 68 | + * type R = AnyType<never> // never |
| 69 | + * type R = AnyType<unknown> // never |
| 70 | + * type R = AnyType<string | boolean> // never |
| 71 | + * ``` |
| 72 | + */ |
| 73 | +export type AnyType<T, Then = T, Else = never> = ... |
| 74 | +``` |
| 75 | +
|
| 76 | +✅ Good |
| 77 | +
|
| 78 | +```ts |
| 79 | +/** |
| 80 | + * Check if the type `T` is exactly `any`. |
| 81 | + * |
| 82 | + * ```ts |
| 83 | + * type R = AnyType<any> // any |
| 84 | + * |
| 85 | + * type R = AnyType<never> // never |
| 86 | + * type R = AnyType<unknown> // never |
| 87 | + * type R = AnyType<string | boolean> // never |
| 88 | + * ``` |
| 89 | + */ |
| 90 | +export type AnyType<T, Then = T, Else = never> = ... |
| 91 | +``` |
| 92 | +
|
| 93 | +[type-plus]: https://github.com/unional/type-plus |
0 commit comments