Typedoc tutorial | Typescript Documentation API generator
- Admin
- Feb 24, 2024
- Typescript
What is typedoc in typescript?
TypeDoc is an API documentation generator designed specifically for TypeScript applications, similar to JSDoc, ESdoc and JavaDoc
.
Comprehensive documentation plays a pivotal role in the successful development of any project.
This tool takes TypeScript files or projects as input, parses them, and generates documentation in various formats, with HTML being one of the primary outputs. The generated HTML includes links, styles, and formatted code snippets obtained by parsing TypeScript elements such as classes, interfaces, and methods.
Installation and Setup of TypeDoc in a TypeScript Application
TypeDoc
can be utilized either as a standalone NPM command line tool or integrated seamlessly with popular build tools like webpack
, gulp
, and grunt
.
Before installing TypeDoc, ensure that Node.js is installed on your system and that both the npm
and n`ode commands are executable.
You can install it either globally or locally. The -g
option will install globally, without the -g
option, and the --save-dev
option installs locally.
To install TypeDoc globally, execute the following command.
npm install -g typedoc
Once installed globally, the typedoc
command becomes available from the terminal. You can verify the installation by running typedoc --version
.
B:\Workspace\blog>typedoc --version
TypeDoc 0.12.0
Using TypeScript 3.0.3 from C:\Users\Kiran\AppData\Roaming\npm\node_modules\typedoc\node_modules\typescript\lib
Alternatively, you can configure TypeDoc
as an npm script within your project’s package.json file:
"scripts":{
"tdocs":"npm run typedocs [options] "
}
After adding this configuration, you can run TypeDoc via the npm command npm run tdocs
.
typedoc command-line Options
TypeDoc’s command-line options are prefixed with a double hyphen --
.
typedoc --options
Some commonly used options include.
Option | Description |
---|---|
theme | Specifies the theme for the generated documentation template, including default, minimal, or custom themes. |
target | Configures the ECMAScript JavaScript versions (ES5, ES6) to compile with. |
out | Specifies the output directory for the generated documentation. |
mode | Determines the output mode of the compiled project, either file or module. |
include | Includes specified files for documentation generation. |
exclude | Excludes specified files from documentation generation. |
Typedoc Generator Example
To explain about TypeDoc’s usage, let’s create a TypeScript file named Animal.ts
with class definitions. Afterward, execute the following command to generate documentation.
class Animal {
public nonveg: boolean;
constructor() {
console.log("new animal created");
}
eat(): void {
console.log("new animal Eat method");
}
}
class Lion extends Animal {
constructor() {
super();
this.nonveg = true;
}
eat(): void {
console.log("Lion Eat method");
}
}
class Cat extends Animal {
constructor() {
super();
this.nonveg = true;
}
eat(): void {
console.log("Cat Eat method");
}
}
How to Generate Html Documentation in Typescript?
Using the typedoc
command, please issue the below command
typedoc --out docs
This command generates HTML documentation in the docs directory.
below the screenshot contains the output-generated documentation.
Typedoc
comes with tdconfig.json
files which contain configuration options required to generate documentation.
This file contains typescript compiler options and include
which files are included to generate Documentation. The Exclude
option excludes the files for generations.
Typedoc Bundler Plugins
TypeDoc seamlessly integrates with popular JavaScript build tools via plugins and npm packages, such as
- Grunt - grunt-typedoc
- Gulp - gulp-typedoc
- Webpack - typedoc-webpack-plugin
Conclusion
TypeDoc
is a powerful npm tool for generating documentation in TypeScript projects. By following these instructions, you can install and configure TypeDoc
in your local environment, enabling you to generate comprehensive documentation for Node.js applications effortlessly.