Today, I’ll tell you how you can simply write a TypeScript NPM package for learning purposes in an hour.
First, let us learn…
Here’s how you can begin with TypeScript
- Open your terminal.
- Create a project folder where suitable.
$ mkdir my-first-typescript-package $ cd my-first-typescript-package $ npm init -y # to skip all the npm questions $ npm i -D typescript
- Following those steps installs you the TypeScript package that enables you to write TypeScript.
touch index.tsin the terminal, and write this one line of code in
- And then, just run
npm run buildto see the output file being generated by the TypeScript module!
node index.jsand see the terminal output
THIS IS TYPESCRIPT!!!.
So, you now know how TypeScript works!
Few best resources to learn TypeScript
- TypeScript official docs https://www.typescriptlang.org/docs/home.html
- TutorialsPoint also have a nice tut https://www.tutorialspoint.com/typescript/index.htm
- Valentinog’s TypeScript tutorial https://www.valentinog.com/blog/typescript/
Once you know the basic TypeScript syntax, we can now publish our own TypeScript NPM package!
Things to know
While you are just learning, it is okay to just run
tsc in the terminal and get the output files to enjoy with.
But when you are building packages, you might have to write a lot many more
npm scripts, you'll have to write
tsconfig(s), you'll have to setup
Prettier and VSCode for better code, and bringing it all together can get tough for beginners.
To read everything about how this setup works in detail, read this medium article.
There are many TypeScript boilerplates on GitHub to start with. For this article, I will use my TypeScript boilerplate that use to kickstart my projects.
To setup, a basic development environment using that boilerplate, run the following in the terminal.
$ git clone https://github.com/KumarAbhirup/typescript-boilerplate my-typescript-package
$ cd my-typescript-package
$ npm i $ npm run dev
And that’s it! You got your TypeScript working!
To see live changes in the console, you’ll first have to run
npm run build:watch in another terminal tab so that TypeScript keeps a watch over the files you edit so that the TypeScript files get compiled in realtime.
Now, open the
If you are using VSCode and have ESLint and Prettier extensions enabled, then you will also see auto-linting working in the editor.
And now you got a good TypeScript environment working!
Publishing to NPM
If you followed the tutorial to this point, the next steps should be a breeze for you! ❤️
Look before you leap.
- Make sure you are using a unique NPM package name in
package.jsonso that when you deploy to NPM, there are no errors and clashes.
- You might also want to bump versions every time you make a new change.
- Use semantic versioning.
- You might want to use more sophisticated ways to bump versions rather than manually bumping version numbers in the
$ npm run ship # `ship` script is defined in the boilerplate
There are better ways of doing it. You can make use of magic scripts in NPM like prepare, prepublishOnly, preversion, version and postversion for automating stuff but that can be the topic of some another medium article.
Congratulations! You did build your first NPM package, and it might have taken you just a few minutes, or an hour or so.
Building your first TypeScript NPM package can be a fun learning project of the day. Keep building more of them!
Originally published at https://dev.to on October 20, 2019.