Add documentation examples#141
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds illustrative TypeScript examples for the useBaseTypeOfLiteral and preferUnionType options in two rule documentation files.
- Introduce examples showing base vs. literal type usage under
useBaseTypeOfLiteral. - Demonstrate union vs. separate
@throwstags underpreferUnionType.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| docs/rules/no-undocumented-throws.md | Added TS examples for useBaseTypeOfLiteral and preferUnionType. |
| docs/rules/check-throws-tag-type.md | Added TS examples for useBaseTypeOfLiteral and preferUnionType. |
Comment on lines
+18
to
+68
| ```ts | ||
| // useBaseTypeOfLiteral: true | ||
| /** | ||
| * @throws {string} | ||
| */ | ||
| function test() { | ||
| throw 'foo'; | ||
| } | ||
|
|
||
| // useBaseTypeOfLiteral: false | ||
| /** | ||
| * @throws {'foo'} | ||
| */ | ||
| function test() { | ||
| throw 'foo'; | ||
| } | ||
| ``` | ||
|
|
||
| ### `preferUnionType` | ||
|
|
||
| Default: `true` | ||
|
|
||
| When more than one exception can be thrown, the types are added as a union type in the comment. | ||
| If set to `false`, each exception is written on its own line. | ||
|
|
||
| ```ts | ||
| // preferUnionType: true | ||
| /** | ||
| * @throws {FooError | BarError} | ||
| */ | ||
| function test() { | ||
| if (Math.random() < 0.5) { | ||
| throw new FooError(); | ||
| } else { | ||
| throw new BarError(); | ||
| } | ||
| } | ||
|
|
||
| // preferUnionType: false | ||
| /** | ||
| * @throws {FooError} | ||
| * @throws {BarError} | ||
| */ | ||
| function test() { | ||
| if (Math.random() < 0.5) { | ||
| throw new FooError(); | ||
| } else { | ||
| throw new BarError(); | ||
| } | ||
| } | ||
| ``` |
There was a problem hiding this comment.
[nitpick] These examples are identical to those in check-throws-tag-type.md. Consider consolidating shared examples or adding a cross-reference to avoid documentation drift.
Suggested change
| ```ts | |
| // useBaseTypeOfLiteral: true | |
| /** | |
| * @throws {string} | |
| */ | |
| function test() { | |
| throw 'foo'; | |
| } | |
| // useBaseTypeOfLiteral: false | |
| /** | |
| * @throws {'foo'} | |
| */ | |
| function test() { | |
| throw 'foo'; | |
| } | |
| ``` | |
| ### `preferUnionType` | |
| Default: `true` | |
| When more than one exception can be thrown, the types are added as a union type in the comment. | |
| If set to `false`, each exception is written on its own line. | |
| ```ts | |
| // preferUnionType: true | |
| /** | |
| * @throws {FooError | BarError} | |
| */ | |
| function test() { | |
| if (Math.random() < 0.5) { | |
| throw new FooError(); | |
| } else { | |
| throw new BarError(); | |
| } | |
| } | |
| // preferUnionType: false | |
| /** | |
| * @throws {FooError} | |
| * @throws {BarError} | |
| */ | |
| function test() { | |
| if (Math.random() < 0.5) { | |
| throw new FooError(); | |
| } else { | |
| throw new BarError(); | |
| } | |
| } | |
| ``` | |
| For examples of how to use the `useBaseTypeOfLiteral` and `preferUnionType` options, refer to the documentation for the `check-throws-tag-type` rule [here](./check-throws-tag-type.md#examples). |
| When a literal value is thrown, document its base type rather than the literal type. | ||
| For example, for `throw "foo"`, insert `@throws {string}` instead of `@throws {"foo"}`. | ||
|
|
||
| ```ts |
There was a problem hiding this comment.
[nitpick] These examples mirror those in no-undocumented-throws.md. To improve maintainability, you might centralize these snippets or link between the two rule docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.