Search Terms
Docs, documentation, re-key, utility, type, extract, literal, update, tsdoc, comment, source, text
Suggestion
I'd love to see documentation as a first-class citizen of the type system. My suggestion is a new utility type Documented, which allows users to document and unwrap documentation at the type-level.
The type could look as follows: Documented<V, D extends string> = V.
This:
typeX={a: Documented<string,"Some documentation for `a`">;};... would be equivalent to this:
typeX={/** * Some documentation for `a` */a: string;};While it would be preferable for consistency to specify the documentation attached to the key (as this is how mapped types transfer documentation), index signature and other contextual restrictions apply. Aka., this is no good:
typeX={[Documented<"a","Some documentation for `a`">]: string;}Additionally, accessing the symbol of the literal of a specific key can be arduous (Extract<keyof ..., ...>). Whereas accessing the symbol of the field is simple (X["a"]).
ALSO: apologies if I'm butchering the terminology.
Use Cases
This utility type would enable users to extract documentation as string literal types, which they could then further manipulate and use elsewhere. They could build up higher-order generic types to assist with and type-check the documentation process.
Examples
typeX={/** * Some documentation for `a` */a: string;};typeXaDocs=X["a"]extendsDocumented<any, infer D> ? D : undefined;declareconstxaDocs: XaDocs;// "Some documentation for `a`"// update (prefix) the string literal type of the documentationtypeXaDocsUpdated=XaDocsextendsstring ? `USE WITH CAUTION: ${XaDocs}` : undefined;// utilize the `Documented` utility to attach the new documentationtypeXUpdated1={a: Documented<string,XaDocsUpdated>;};// perhaps within a mapped typetypeXUpdated2={[KinkeyofX]: Kextends"a" ? Documented<X[K],XaDocsUpdated> : X[K];};In either of the cases above, a will be documented as "USE WITH CAUTION: Some documentation for a".
The real power of this utility is not in direct usage as demoed above, but rather in higher-order utilities. For instance, you could imagine a utility library that lets users specify flags that correspond to expected documentation.
import{EnsureDocumentationExists}from"some-doc-utility-lib";import{HasDescriptionAndExampleDocs,HasDescriptionDocs}from"./my-own-potentially-poorly-documented-types";exporttypeA=EnsureDocumentationExists<HasDescriptionAndExampleDocs,{description: true;example: true}>;exporttypeB=EnsureDocumentationExists<HasDescriptionDocs,{description: true;example: true}>;// type-errorexporttypeC=EnsureDocumentationExists<HasDescriptionDocs,{description: true}>;Checklist
My suggestion meets these guidelines:
Search Terms
Docs, documentation, re-key, utility, type, extract, literal, update, tsdoc, comment, source, text
Suggestion
I'd love to see documentation as a first-class citizen of the type system. My suggestion is a new utility type
Documented, which allows users to document and unwrap documentation at the type-level.The type could look as follows:
Documented<V, D extends string> = V.This:
... would be equivalent to this:
While it would be preferable for consistency to specify the documentation attached to the key (as this is how mapped types transfer documentation), index signature and other contextual restrictions apply. Aka., this is no good:
Additionally, accessing the symbol of the literal of a specific key can be arduous (
Extract<keyof ..., ...>). Whereas accessing the symbol of the field is simple (X["a"]).Use Cases
This utility type would enable users to extract documentation as string literal types, which they could then further manipulate and use elsewhere. They could build up higher-order generic types to assist with and type-check the documentation process.
Examples
In either of the cases above,
awill be documented as "USE WITH CAUTION: Some documentation fora".The real power of this utility is not in direct usage as demoed above, but rather in higher-order utilities. For instance, you could imagine a utility library that lets users specify flags that correspond to expected documentation.
Checklist
My suggestion meets these guidelines: