Is there a way to document fields in an interface in TypeScript?
Asked Answered
C

3

49

Say I have the following:

interface Validator {
  validate: (value: string) => boolean;
  errorMessage: string;
}

interface EditDialogField {
  label: string;
  prop: string;
  required?: boolean;
  type: 'input';
  validators?: Validator[];
}

This is useful, as IntelliSense pops up suggestions when I use these interfaces, but I'd like the ability to add comments that also show up in IntelliSense (specifically VS Code). Is this possible?

Creel answered 1/8, 2018 at 19:57 Comment(1)
Possible duplicate of Where is the syntax for TypeScript comments documented?Enmesh
C
105

Got it!

interface EditDialogField {
  /** Explain label here */
  label: string;
  /** Explain prop here */
  prop: string;
  required?: boolean;
  type: 'input';
  validators?: Validator[];
}
Creel answered 1/8, 2018 at 20:1 Comment(4)
Can you please confirm if it works with // only? I guess /** is the only valid syntax?Peregrine
can confirm this works in VSCode, but couldn't find it documented on tsdoc.orgSlovakia
/** comment */ DOES show up in intelliJ IDEA when Editor > Appearance > Show documentation popup is checked.Flashback
What setting in VSCode enables those to show as hover hint?Schober
M
35

If you want to see it appearing when your mouse is hover in your editor, I suggest you document the interface and use the @field inside the documentation comment.

Alternatively you could use @member maybe that gives a better syntax-highlighting for the doc

/**
 * This is the description of the interface
 *
 * @interface EditDialogField
 * @member {string} label is used for whatever reason
 * @field {string} prop is used for other reason
 */
interface EditDialogField {
  label: string;
  prop: string;
  required?: boolean;
  type: 'input';
  validators?: Validator[];
}

Result inside VSCode enter image description here

Mandeville answered 20/8, 2020 at 13:25 Comment(2)
I see it when I hover over the interface. However, when I hover over the destructured vars when I use it the information does not appear. However, using @Creel approach shows the information when hovering destructured vars but not over the interface.Foreground
Could you link to documentation legitimizing these tags? I don't see them in TSDoc; is there another standard?Subway
F
7

I want to build upon @Ronan Quillevere's and @ffxsam's answers.

Ronan's approach shows information in VSCode when hovering over the interface as shown in his comment.

However, when using that interface as in the following example, hovering over the destructured vars of the last line does not show the documentation from the member/field tag, but from the comment inside the interface, as ffxsam suggested.

/**
 * This is the description of the interface
 *
 * @interface EditDialogField
 * @member {string} label is used for whatever reason
 * @field {string} prop is used for other reason
 */
 interface EditDialogField {
  /** Doc inside the interface */ 
  label: string;
  prop: string;
  required?: boolean;
  type: 'input';
}

const dialog: EditDialogField = { label: 'label', prop: 'prop', type: 'input' };
const { label, prop } = dialog;

These pics better show the behavior in VSCode. enter image description here

enter image description here

I'm not sure if there is way to unify this, but it would be great

Foreground answered 27/9, 2022 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.