This content originally appeared on DEV Community and was authored by Amel Spahić
amelspahic / typescript-library-template
Initial boilerplate for TypeScript libraries including tests, coverage, documentation.
typescript-library-template
A starter template for TypeScript libraries. Use this repository as a starting point for your own TypeScript library. This template includes the following features:
- Compiles TypeScript code using both the
tsconfig.json
andtsconfig.module.json
files. - Formats TypeScript code using Prettier.
- Lints TypeScript code using ESLint.
- Runs unit tests using AVA.
- Generates code coverage reports using NYC.
- Generates HTML documentation using TypeDoc.
- Uses Husky Git hooks and Lint-staged pre-commit hooks.
Installation
Clone the repository:
git clone https://github.com/amelspahic/typescript-library-template.git
Install the dependencies:
npm install
There are several scripts available to help you get started:
Compile the TypeScript code using both the tsconfig.json
and tsconfig.module.json
files.
npm run build
Formats the TypeScript code using Prettier and lints the code using ESLint, fixing any issues found.
npm run fix
Lints the TypeScript code using ESLint, checks the code formatting using Prettier, and runs the unit tests using AVA.
npm run test
TypeScript is a popular and influential language that extends JavaScript with static typing and other features. It can help you write more reliable, maintainable, scalable code for your projects. However, creating and publishing a TypeScript library can be challenging, especially if you are new to the TypeScript ecosystem. Many tools and configurations are involved, and finding a good template or guide that covers everything you need is challenging.
For the reasons mentioned above, I created a TypeScript library template you can use as a starting point for your library. It is based on my experience and best practices from the TypeScript community (this is usually subjective), and it includes everything you need for developing, testing, documenting, and publishing your library. This article will explain what this template offers, how to use it, and how to customize it for your needs.
What is typescript-library-template?
The typescript-library-template is a GitHub repository that you can use as a template for creating your own TypeScript library. It has the following features:
TypeScript: as the primary language for writing your library code. It also includes some recommended settings for the
tsconfig.json
andtslint.json
files.AVA: as the testing framework for writing and running unit tests for your library. It also supports code coverage reports with Istanbul (NYC).
Typedoc: as the documentation generator for creating API documentation for your library from your TypeScript comments.
Prettier: as the code formatter for keeping your code style consistent and readable.
ESLint (typescript-eslint): for static code analysis.
Husky and Lint-staged: The template uses Husky and Lint-staged to run pre-commit hooks that ensure your code is formatted, linted, tested, and documented before committing.
How to use typescript-library-template?
I will mention just a couple of commands below. You can find more information in the README.md file.
Using typescript-library-template is very easy. You need to follow these steps:
Click the "Use this template" button on the GitHub repository page. This will create a new repository based on this template under your account.
Clone or download your new repository to your local machine.
Run
npm install
oryarn install
to install all the dependencies.Start developing your library by editing the
src/index.ts
file (or adding more files undersrc
). Using AVA syntax, you can write unit tests using.spec.ts
or.test.ts
suffixes.Run
npm run build
oryarn build
to build your library. This will generate multiple output files under thedist
folder.Run the
npm run test
oryarn test
to run all unit tests using AVA.To generate a code coverage report under the coverage folder, run
npm run cov
.Run
npm run doc
oryarn doc
to generate API documentation using Typedoc. This will create an HTML file underdocs/index.html
.Run
npm run fix
oryarn fix
to format all source files using Prettier.Commit your changes (it will activate a pre-commit hook that will perform test and linting/formatting)
Push your changes to GitHub.
How to customize typescript-library-template?
You can customize the typescript-library-template according to your preferences. Add files, change configurations, and remove whatever you find unimportant to your liking.
This content originally appeared on DEV Community and was authored by Amel Spahić
Amel Spahić | Sciencx (2023-03-07T22:57:23+00:00) How to create and publish a TypeScript library with ease. Retrieved from https://www.scien.cx/2023/03/07/how-to-create-and-publish-a-typescript-library-with-ease/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.