Skip to content

Support for comments inside JSDoc @example comments #52398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
5 tasks done
ackvf opened this issue Jan 24, 2023 · 4 comments
Open
5 tasks done

Support for comments inside JSDoc @example comments #52398

ackvf opened this issue Jan 24, 2023 · 4 comments
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@ackvf
Copy link

ackvf commented Jan 24, 2023

Suggestion

πŸ” Search Terms:

comments inside jsdoc, jsdoc inception

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I am writing a custom helper type that allows me to provide JSDoc comments for properties of any other object or interface that is inaccessible for changes.
I need to document the feature and usage, but I can't close a comment section inside JSDoc comment's.

Code:

image

/**
 * This type allows to add JSDoc annotation to any type without causing conflicts.
 *
 * @example
 * type IconButtonProps =
 *  & JSDoc
 *  & ShorthandVariantsAndSizes
 *
 * type JSDoc = Documentation<ShorthandVariantsAndSizes, {
 *  /**
 *   * **variant**: Color theme for the button.
 *   * - `light` variant is white on all themes.
 *   \*\/
 *   variant?: unknown  // <- note: this `?: unknown` optional type is important
 * }>
 */
type Documentation<T, TargetType extends { [K in keyof T]?: unknown }> = TargetType

JSDoc invoked:

image

πŸ’» Use Cases

Desired:

image

Result:

image

πŸ“ƒ Motivating Example

const sizeKeys = ['small', 'large'] as const

// this is a mapped type and therefore doesn't allow to add JSDoc comments
type ExplodedSizes = { [key in typeof sizeKeys]?: unknown }

type JSDoc = Documentation<ExplodedSizes, {
  /**
   * **size**: Button size.
   * - `64px`
   */
  large?: unknown
  /**
   * **size**: Button size.
   * - `32px`
   */
  small?: unknown
}>

// this merges the jsdoc annotation with correct types
export type IconButtonProps =
  & JSDoc
  & ExplodedSizes

export const IconButton: React.FC<IconButtonProps> = (props) => <> </> 

// later when using the component
const render = <IconButton /> // <- gets correct intellisense with JSDoc comments here

image

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript In Discussion Not yet reached consensus Domain: JSDoc Relates to JSDoc parsing and type generation labels Jan 24, 2023
@DanielRosenwasser
Copy link
Member

#47679 is related. #50677 has a tenative fix, but we haven't discussed it much recently. I don't recall if it supports \ escaping.

@ackvf
Copy link
Author

ackvf commented Jan 25, 2023

@DanielRosenwasser I don't think it does or ever did, but it was the most intuitive solution for the reader when I accept that it doesn't work. Using non-functioning escaping still conveys the message to the next developer who reads the documentation, so it was the best solution.

I tried various different combinations with spaces, multiple * or multiple /, etc.

@ackvf
Copy link
Author

ackvf commented Mar 29, 2023

Heads up, I found a neat workaround that is not ideal due to syntax highlight, but it does produce a little clearer output.

Solution lies in replacing * with β˜… or ⋆, see also https://www.symbolcopy.com/star-symbol.html

/**
 * This type allows to add JSDoc annotation to any type without causing conflicts.
 *
 * @example
 * type IconButtonProps =
 *  & JSDoc
 *  & ShorthandVariantsAndSizes
 *
 * type JSDoc = Documentation<ShorthandVariantsAndSizes, {
 *  /β˜…β˜…
 *   * **variant**: Color theme for the button.
 *   * - `light` variant is white on all themes.
 *   β˜…/
 *   variant?: unknown  // <- note: this `?: unknown` optional type is important
 *   // ... other props
 * }>
 */
type Documentation<T, TargetType extends { [K in keyof T]?: unknown }> = TargetType

image
image
The result is that the "comment" now looks weird with that syntax highlight, but the important bit, "variant?: unknown" doesn't look as a comment as in original screenshot and looks as a property.


So, if I were to choose between those two, I would pick the latter.
image
image

@jaydenseric
Copy link

See prior discussion microsoft/tsdoc#166 (comment) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: JSDoc Relates to JSDoc parsing and type generation In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants