Skip to content
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

Support documentation for string literal types in unions #1148

Closed
1 task done
socsieng opened this issue Dec 19, 2019 · 5 comments
Closed
1 task done

Support documentation for string literal types in unions #1148

socsieng opened this issue Dec 19, 2019 · 5 comments
Labels
enhancement Improved functionality

Comments

@socsieng
Copy link
Contributor

Problem

Normally I would use an enum for this, however, I am documenting an existing JavaScript library as a .d.ts file where the actual type doesn't exist in JavaScript.

There doesn't appear to be any support for documenting or applying an @tag to individual string literals in a type union.

Example:

type CallbackType = /** Fired when ready */ 'ready' |
  /** Fired when complete */ 'complete' |
  /** Fired when an error occurs */ 'error' |
  /** @hidden */ 'unknown';

The above example has no effect on the generated documentation.

Suggested Solution

Add support for documenting individual items in a union type including @tags.

@Gerrit0 Gerrit0 added the enhancement Improved functionality label Dec 21, 2019
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Dec 21, 2019

This gets complicated quickly. Right now we don't support documenting any parts of types except for properties of interfaces. If we add support for members of a union, it seems reasonable to also make it possible to document parts of an intersection, properties within those parts... etc. I'm also not really sure how this extra documentation would be rendered.

An alternative method of documenting this could be:

interface Callbacks {
  /** Fired when ready */
  ready: never;
  /** Fired when complete */
  complete: never;
  /** Fired when an error occurs */
  error: never;
  /** @hidden */
  unknown: never
}

type CallbackType = keyof Callbacks

@socsieng
Copy link
Contributor Author

Thanks @Gerrit0.

I ended up going with a slight variation on this:

type Callbacks = {
  /** Fired when ready */
  ready: never;
  /** Fired when complete */
  complete: never;
  /** Fired when an error occurs */
  error: never;
  /** @hidden */
  unknown: never
}

type CallbackType = keyof Callbacks

I preferred the output of the documentation following a type declaration instead of an interface one.

In either case, these are the results that I've observed:

  • VSCode: Displays CallbackType values as union of strings (expected)
  • VSCode: Doesn't honor the @hidden tag (unexpected)
  • TypeDoc: Honors the @hidden tag (expected)
  • TypeDoc: Doesn't automatically link to the Callbacks type (unexpected)

Further to the last point, the output of the CallbackType documentation reads as follows:

T CallbackType: keyof Callbacks

But I expected Callbacks to be hyperlinked like this:

T CallbackType: keyof [[Callbacks]]

It's not 100%, but it is better than nothing.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Dec 23, 2019

The @hidden annotation is TypeDoc specific. If you want to remove something from a declaration file after build so that it doesn't show up in VSCode you'll need @internal (which TypeDoc supports, if you pass --stripInternal)

Not linking to callbacks in keyof Callbacks sounds like a bug to me.

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Feb 12, 2020

You might be interested in microsoft/tsdoc#164, if a solution is discovered there I'd like to revisit this.

@socsieng
Copy link
Contributor Author

Cool. Thanks for the share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improved functionality
Projects
None yet
Development

No branches or pull requests

2 participants