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

RFC: Specify that doc comments are allowed on simple union types #164

Open
biw opened this issue Jun 25, 2019 · 12 comments
Open

RFC: Specify that doc comments are allowed on simple union types #164

biw opened this issue Jun 25, 2019 · 12 comments
Labels
request for comments A proposed addition to the TSDoc spec

Comments

@biw
Copy link

biw commented Jun 25, 2019

Hello,

I can't find any documentation on adding tsdocs to a typescript string literal type. I was wondering if the following is possible:

type InputType = 
/** the TSDoc doesn't work for num1, but I'd like it to */
| 'num1'
/** the TSDoc doesn't work for num2, but I'd also like it to */
| 'num2'

export const test = (...input: InputType[]) => {
    return input
}

// I'd like to get the TSDoc from 'num1' string literal to show up
// when inputting it into the `tac` function but it does not currently show
test('num1')

I'm not 100% tied to using string literal types, so if there is another way to do this with enums, etc., I'd love to learn 😄

If the above is not currently possible, I'd love to work to add it to tsdoc (with some guidance 😄).

Thanks!

@octogonz
Copy link
Collaborator

Does the compiler preserve these comments in the emitted .d.ts files? I recall sometimes comments in certain places get discarded.

TSDoc itself doesn't (yet) dictate very specifically where comments can appear. That is determined by the documentation tool that uses the TSDoc library. Which tool are you using?

@christianalfoni
Copy link

christianalfoni commented Feb 12, 2020

Hi there, I have the same thing. I am using String Literal types to define classnames and would love to populate with some more information.

So basically:

type TClassnames = 
	| 'bg-color-red-500'
    | 'bg-color-red-600'

I would love to populate the docs on the right side of the intellisense:

Screenshot 2020-02-12 at 07 31 36

This is how we can document with properties:

Screenshot 2020-02-12 at 10 10 59

Which would be amazing to have on string literals

@littlecold233
Copy link

Any progress on this issue?

@octogonz
Copy link
Collaborator

octogonz commented Apr 16, 2020

To answer my earlier question, the TypeScript compiler does preserve these comments in the .d.ts file:

export declare type InputType = 
/** the TSDoc doesn't work for num1, but I'd like it to */
'num1'
/** the TSDoc doesn't work for num2, but I'd also like it to */
 | 'num2';

So I think this feature seems pretty reasonable. 👍

But as mentioned previously, the TSDoc standard can specify that "yes this is an appropriate place to put a comment", but the TSDoc library only parses comments. It does not parse API signatures. That part is handled by your documentation tool such as API Extractor or TypeDoc.

So you'd need to open an issue to request this feature for the documentation tool.

@octogonz octogonz added the request for comments A proposed addition to the TSDoc spec label Apr 16, 2020
@octogonz octogonz changed the title TSDocs on TypeScript String Literal Types RFC: Specify that doc comments are allowed on simple union types Apr 16, 2020
@Gerrit0
Copy link
Contributor

Gerrit0 commented Apr 20, 2020

If this is specified as valid, what would documented types look like? Right now both api-extractor and typedoc just display the type. Do we need special casing for unions? Are doc comments allowed on union types within another type? Would this result in users expecting to be able to include comments almost anywhere within a type and have them be picked up?

type Messy = [ /** a */ 1, /** b */ 2 | /** c */ 3]

@octogonz
Copy link
Collaborator

@Gerrit0 my thinking to restrict this feature to a very special pattern that is commonly used like an enum. We could give it a name like "union enum".

For example, these would be union enums:

/**
 * A standard system color.
 */
export type Color = 
  /**
   * The color `#ff0000`
   */
  'red' | // do we care whether "|" is before/after the comment? Not really
  /**
   * The color `#00ff00`
   */
  'green' 
  /**
   * The color `#0000ff`
   */
  | 'blue';

/**
 * A binary digit.
 */
export type Bit = /** zero */  0  |  /** one*/ 1;

But it's optional. For example, maybe a documentation tool only treats a type as an "union enum" if at least one of the members has a /** */ comment.

And it would NOT apply to any other generalized expressions, not even this sort of thing:

// NOT a union enum:
type Mammal = Cat | Dog | Horse;

For the most part, such a feature would be a feature request for your particular documentation tool. If TSDoc "supports" this feature, its role is merely to:

  • specify that it's "standard" to put doc comments on these subexpressions
  • provide a declaration reference notation for referring to them

@TheMrZZ
Copy link

TheMrZZ commented Sep 15, 2020

It looks like this feature was accepted, when looking at the Version 1.0 tracking. According to Gerrit0's comment, it should be available by the end of the year if I understood correctly!

Edit: I misunderstood - see the comment below.

@Gerrit0
Copy link
Contributor

Gerrit0 commented Sep 15, 2020

Note: TypeDoc != TSDoc. This hasn't been added to the TSDoc standard yet, but TypeDoc plans to support it. Other documentation generators might not necessarily support it.

@TheMrZZ
Copy link

TheMrZZ commented Sep 19, 2020

Note: TypeDoc != TSDoc. This hasn't been added to the TSDoc standard yet, but TypeDoc plans to support it. Other documentation generators might not necessarily support it.

Therefore, it was the 2nd case - me misunderstanding this basic thing. Sorry for the mistake, and thanks for the clarification!

As it is related, I'll rewrite below the comment I made on the official TypeScript repo.

This is an underrated feature. Nowadays, a lot of enum are done using string literals - a powerful feature that puts Typescript above a lot of static languages. However, those enumerations lack proper documentation, which causes several problems.

Here are a few real-world use-cases:

React Native CSS

<Text style={{
  color: 'blue',
  alignSelf: 'center',
}}> This is a blue text </Text>

Adding string comments would simplify the understanding of each possibility (each color, each possible alignments).

This is applicable to all properties with a defined set of string values.

Configuration files

For example, let's take a tsconfig.json file:

{
  "module": "commonjs"
}

Currently, the documentation looks like this:
Capture d’écran de 2020-09-16 13-19-44

While we know all the possible options, there is no way to understand what they actually do. String comments could tell explicitly the user what each option is supposed to change.

This is applicable to all configuration files that are made with TS, not only tsconfig.json.

Material-UI

Material-UI relies a lot on string enumerations, but they are not always easy to understand.

<Button variant="contained"> My Button </Button>

In the above example, while variant is trivial to understand, it's not easy to guess what the "contained" value means. You have to rely on the external documentation, where that shouldn't be necessary.

This is applicable to all React props with a defined set of string values.

In conclusion, I think it's a very good idea, and it could simplify documentation in a lot of projects.

@pangxiaoli
Copy link

Any progress on this issue today, plz

@55Cancri
Copy link

Is this feature difficult to implement?

@jonnyasmar
Copy link

FWIW, this doesn't appear to work for complex union/intersection types either until you've defined enough properties to resolve the full type, e.g.

export type Config = {
  /**
  * Testing
  */
  name: string;
  abstract?: boolean;
} & (
  | {
      storage?: Storage.Stream;
    }
  | {
      storage?: Storage.Dynamo;
      table?: string;
    }
);

const config1: Config = {
  name: "test" // no comment in tooltip
}

const config2: Config = {
  name: "test", // comment appears in tooltip
  storage: Storage.Stream
}

jwatzman pushed a commit to getcord/sdk-js that referenced this issue Sep 20, 2023
When adding the `resolvedStatus` to the LocationData API, I noticed that it wasn't relevant
to the ThreadList, as the ThreadList doesn't have pagination and fetches all threads anyway - doing
all filtering within the component. So I created a union type. This is not great 1. because it doesn't
enforce consistency in our API filters, and 2. because the docs look bad (I had not noticed). TsDoc
can't handle union types and it's a known bug (microsoft/tsdoc#164). This
PR moves the `resolvedStatus` within the ThreadListFilter type, then goes through all its uses and
decides on whether to omit it or not.

Test Plan:
Tested ThreadedComments with all 4 tab options and they worked correctly. Then tested the
ThreadedComments by switching the underlying API to use the deprecated parameter, which also worked
as expected.

Also, docs look really nice again :)


Reviewers: flooey, jwatzman, km-nur

Reviewed By: km-nur

Pull Request: https://github.com/getcord/monorepo/pull/6436

monorepo-commit: e8a3e6ed8846cff9baeffc16e7e1dabdb7e42cb7
peter-pakanun added a commit to peter-pakanun/pathofexile-types that referenced this issue Mar 29, 2024
just wait for tsdoc to support template literal
microsoft/tsdoc#164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request for comments A proposed addition to the TSDoc spec
Projects
None yet
Development

No branches or pull requests

9 participants