Make your npm package conformable to TypeScript

So last time we made an NPM package with JavaScript.

Change your index.js file to index.ts

Just change the extension of the file and update the source code.

Initialize with tsc command

Let’s initialize your project with tsc command, which generates tsconfig.json file.

$ tsc --init
message TS6071: Successfully created a tsconfig.json file.

Add "declaration": true to your tsconfig.json

We should do this to generate a tsconfig.json file when we execute yarn build.

"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"strict": true,
"esModuleInterop": true
"exclude": [

Add "types": "index.d.ts" to your package.json

By adding this, a type definition file will be generated as index.d.ts.

"name": "@taishikato/slug-generator",
"version": "2.2.0",
"description": "generate slug string",
"main": "index.js",
"types": "index.d.ts",
"repository": "",
"author": "taishikato",
"license": "MIT",
"private": false,
"scripts": {
"build": "tsc"
"dependencies": {
"uuid": "^7.0.2"
"keywords": [
"slug generator"
"devDependencies": {
"@types/uuid": "^7.0.2",
"typescript": "^3.8.3"

Add .npmignore

This file is the key.


taishikato/slug-generator: Slug generator for blog posts or any other user-generated-contents


Trying to start a company in Vancouver