Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 398
ffxsam Avatar asked Aug 01 '18 19:08

ffxsam


People also ask

How do you document an interface?

Documenting an interface consists of naming and identifying it and documenting its syntactic and semantic information. The first two parts constitute an interface's "signature." When an interface's resources are invokable programs, the signature names the programs and defines their parameters.

Can TypeScript interface have methods?

A TypeScript Interface can include method declarations using arrow functions or normal functions, it can also include properties and return types. The methods can have parameters or remain parameterless.

What is the point of interface in TypeScript?

Interfaces in TypeScript have two usage scenarios: you can create a contract that classes must follow, such as the members that those classes must implement, and you can also represent types in your application, just like the normal type declaration.

How do you define an array in TypeScript interface?

To define an interface for an array of objects, define the interface for the type of each object and set the type of the array to be Type[] , e.g. const arr: Employee[] = [] . All of the objects you add to the array have to conform to the type, otherwise the type checker errors out.


1 Answers

Got it!

interface EditDialogField {   /** Explain label here */   label: string;   /** Explain prop here */   prop: string;   required?: boolean;   type: 'input';   validators?: Validator[]; } 
like image 83
ffxsam Avatar answered Sep 28 '22 20:09

ffxsam