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": [
"node_modules",
"dist"
]
}

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": "https://github.com/taishikato/slug-generator",
"author": "taishikato",
"license": "MIT",
"private": false,
"scripts": {
"build": "tsc"
},
"dependencies": {
"uuid": "^7.0.2"
},
"keywords": [
"slug",
"npm",
"package",
"taishikato",
"slug generator"
],
"devDependencies": {
"@types/uuid": "^7.0.2",
"typescript": "^3.8.3"
}
}

Add .npmignore

This file is the key.

.gitignore
yarn.lock
node_modules
index.ts

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

Portfolio

Trying to start a company in Vancouver https://taishikato.com