DEV Community

Cover image for Start a new library with TS in under 30 seconds
Darlan Alves
Darlan Alves

Posted on • Updated on

Start a new library with TS in under 30 seconds

Have an awesome idea you wanna try?

Great!

You can start coding it in under 30 seconds with Typescript, Jasmine, TDD and even a coverage report!

mkdir -p idea/src
cd idea
npm init -y && npm i -D @types/jasmine \
 ts-node typescript nodemon nyc jasmine
Enter fullscreen mode Exit fullscreen mode

Now open package.json and add this to the "scripts" section:

...
"scripts": {
  "test": "./node_modules/.bin/ts-node ./node_modules/jasmine/bin/jasmine --config=./jasmine.json",
  "tdd": "./node_modules/.bin/nodemon -w src -e ts -x ./node_modules/.bin/ts-node ./node_modules/jasmine/bin/jasmine --config=./jasmine.json",
  "coverage": "./node_modules/.bin/nyc -r html -e .ts -x \"src/**/*.spec.ts\" npm run test",
  "build": "tsc --project ./tsconfig.json"
}
...
Enter fullscreen mode Exit fullscreen mode

Doing good!

Now create a jasmine.json

{
  "spec_dir": "src",
  "spec_files": ["**/*.spec.ts"]
}
Enter fullscreen mode Exit fullscreen mode

Still counting? Okay, last one!

Create a file called tsconfig.json with this:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es2019",
    "lib": ["ESNext", "DOM"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "typeRoots": ["./node_modules/@types"],
    "types": ["jasmine"],
    "outDir": "./dist"
  },
  "include": ["./src/**/*.ts"]
}
Enter fullscreen mode Exit fullscreen mode

You did it!

Now create a folder named src/, run the test suite with npm run tdd and start coding!

When you're ready to show it to the world, npm run build and publish to NPM :)

And make sure your package.json is up to date! Change name, version and main file if needed.

By the way, did you know you can also automate your releases to npm?

Top comments (2)

Collapse
 
iminside profile image
Dani Chu

Hey, there is an even easier option tsdx

Collapse
 
darlanalves profile image
Darlan Alves

Indeed, Denis. And it's a great tool for more serious stuff.

Though, I'd still say, for simpler stuff I'd skip this long list of dependencies when I just need to create a simple library.