You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RFC: Specify that doc comments are allowed on simple union types #164
I can't find any documentation on adding tsdocs to a typescript string literal type. I was wondering if the following is possible:
typeInputType=/** 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'exportconsttest=(...input: InputType[])=>{returninput}// 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 showtest('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 😄).
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?
I would love to populate the docs on the right side of the intellisense:
This is how we can document with properties:
Which would be amazing to have on string literals
Reacted by Florian ERNST, Peter Franken, Arihant Verma, Mo Doaie, Vitor Buzinaro, Captain Pangyo, Fernando Rojo, Abdurahmon Shayxislomov, Callum, Abhijeet Singh and 16 more
Reacted by Julio M Saito, Florian ERNST, Arihant Verma, Leo Weigand, Samantha Branham, Lawrence Cushman and chbyb
To answer my earlier question, the TypeScript compiler does preserve these comments in the .d.ts file:
exportdeclaretypeInputType=/** 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.
Reacted by Florian ERNST, Sepehr Soltanieh, Wildan Zulfikar and Qy
changed the title [-]TSDocs on TypeScript String Literal Types[/-][+]RFC: Specify that doc comments are allowed on simple union types[/+]on Apr 16, 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?
Gerrit Birkeland (@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. */exporttypeColor=/** * 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. */exporttypeBit=/** 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:typeMammal=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
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!
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.
Reacted by Ben Williams, ExE Boss and Florian ERNST
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!
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
<Textstyle={{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:
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.
<Buttonvariant="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.
Reacted by Vitor Buzinaro, Orkhan Alikhanov, Andrew Curtis, Aleksandr Plokhikh, Alexander Prokhorov and Arya Emami
Activity
octogonz commentedon Jun 25, 2019
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 commentedon 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:
I would love to populate the docs on the right side of the intellisense:
This is how we can document with properties:
Which would be amazing to have on string literals
littlecold233 commentedon Apr 16, 2020
Any progress on this issue?
octogonz commentedon Apr 16, 2020
To answer my earlier question, the TypeScript compiler does preserve these comments in the .d.ts file:
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.
[-]TSDocs on TypeScript String Literal Types[/-][+]RFC: Specify that doc comments are allowed on simple union types[/+]Gerrit0 commentedon 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?
octogonz commentedon Apr 24, 2020
Gerrit Birkeland (@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:
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:
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:
TheMrZZ commentedon 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 commentedon 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 commentedon Sep 19, 2020
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
enumare 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
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.jsonfile:Currently, the documentation looks like this:

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.
In the above example, while
variantis 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 commentedon Aug 9, 2022
Any progress on this issue today, plz
55Cancri commentedon Oct 23, 2022
Is this feature difficult to implement?
jonnyasmar commentedon Feb 10, 2023
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.
Moving the resolvedStatus within the ThreadListFilter type
do not use enum