Write a library using TypeScript library starter
Tags: typescript, rollupjs, babel, library
Published: 2017-04-01 12:33:55

---
title: Write a library using TypeScript library starter
published: true
description: Find out how to write a TypeScript library by using a starter kit
cover_image: https://thepracticaldev.s3.amazonaws.com/i/o6v2hup599bnbqlalxwd.jpg
tags: TypeScript, RollupJS, Babel, Library 
---

_Explained in a [video lesson on Egghead](https://egghead.io/lessons/typescript-create-a-typescript-library-using-typescript-library-starter)_ 

Have you ever written a library in JavaScript or TypeScript? Are you planning to do it? If so, you must try [Typescript library starter](https://github.com/alexjoverm/typescript-library-starter), a starter kit that will make easy to get started while providing all features you know to write a library in Typescript.

Writing a library or project always requires some work:

 - Prepare configuration: package.json, tsconfig.json, CI, bundling, build, minification, types generation...
 - Testing tools: Unit test (with watch mode), coverage...
 - Release versions and changelog: This is a repetitive work that takes some time
 - API Docs generation

[Typescript library starter](https://github.com/alexjoverm/typescript-library-starter) solves all of that and more, plus:

 - Uses latest tools: RollupJS (with [tree-shaking](https://alexjoverm.github.io/2017/03/06/Tree-shaking-with-Webpack-2-TypeScript-and-Babel/)), Prettier, Jest, TSLint, Typedoc...
 - Multiple bundle generation, as described in [2ality](http://2ality.com/2017/04/setting-up-multi-platform-packages.html)
 - Focused on automation: with a set of scripts you as a user have all the work done
 - Allows automatic releases to Github and Npm by using and configuring Semantic Release
 - Is already configured to work on Travis, Coveralls, generate types, and more!

Let's see how easy is to use it.

## Get started

Let's start by running:

```
git clone https://github.com/alexjoverm/typescript-library-starter.git
cd typescript-library-starter
npm install
```

A prompt will show up asking your library name:

![Initial prompt](https://thepracticaldev.s3.amazonaws.com/i/3rylupedav8026le066s.png)

When you do so, it will configure the project for you based on the library name and your git config. It re-inits the git directory, configures rollup, package.json and your entry files (both src and test).

See it yourself! Try running `npm t`, it will run a sample test. Or run `npm run build`, it will create a `dist` folder with the bundle, typings file and documentation.

![Dist folder](https://thepracticaldev.s3.amazonaws.com/i/l09lupzu2ep9ya9al1s7.png)

See all the [commands available on the repo Readme](https://github.com/alexjoverm/typescript-library-starter#npm-scripts)

Now start coding. You'll see when you make a commit, your code is automatically formatted! That's thanks to Prettier :)


## Automatic releases and Travis CI

We need to do some previous steps. First, make sure you've created an account on:

 - npm
 - Travis
 - Coveralls

Also, make sure you've set the `repository.url` property on your `package.json`.

Now, run the following script:

```
node tools/semantic-release-prepare
```

The script will setup 2 githooks: commitmsg and prepush. They're convenient, since [semantic release](https://github.com/semantic-release/semantic-release) works by following [conventional commit messages](https://github.com/conventional-changelog/conventional-changelog).

At this point, you just need to install semantic release. **Answer NO** to the "Generate travis.yml" question, because it is already prepared for you.

```
npm install -g semantic-release-cli
semantic-release setup
```

From now on, better use `npm run commit`. It will show you a prompt which is very convenient for creating a conventional commit. Note that running `git commit -a` without the proper commit message will fail.

![Conventional commit](https://thepracticaldev.s3.amazonaws.com/i/odilqrbecojf0hjaaqx2.png)

You'll notice that when you run `git push`, the exactly same command that Travis CI runs is run locally on your machine, so you can see early if the build will fail.

## Wrapping up

We haven't dived into any kind of configuration, but we were able to not only write a dummy library, but also make sure all quality steps (linting, testing, coverage) are asserted and that we can create bundles, builds, typings and all things needed for a TypeScript library.

Not only that, but also how to make maintainability easy by automating releases!

If you liked [Typescript library starter](https://github.com/alexjoverm/typescript-library-starter), please give it a star. If you have any feedback, you can either open an issue or reach me at [@alexjoverm](https://twitter.com/alexjoverm)