- Create a new package project in a new folder
npm init -y
- Create an
src
folder and aindex.ts
file inside it.
We will start by installing tsx
.
It uses esbuild
to run typescript files on the fly in node.js
with zero configuration.
-
run
npm install -D tsx
-
add a
dev
script topackage.json
to runtsx
inwatch
mode."scripts": { "dev": "tsx watch ./src/index.ts", }
-
Write some typescript code in
index.ts
and runnpm run dev
to see it in action.
While esbuild
is orders-of-magnitude faster then tsc
it doesn't type-check our code.
Although there are configuration options to use tsc only for type-checking in tandam with esbuild
for the actual compilation, I want to keep our config simple and not rely too much on esbuild
other then spontanious zero-config development. So let's use tsc
to build our package for production.
-
install the following
dev dependencies
- typescript - the
tsc
compiler for our build step - @types/node - types for node.js
- rimraf - a
rm -rf
util for node.js that works on windows too
- typescript - the
-
Add a
tsconfig.json
file to the root of the project with the following code. If you're curious about the configuration options, you can read about them here, or runtsc --init
to generate atsconfig.json
file with comments explaining each option...{ "compilerOptions": { "target": "esnext", "module": "esnext", "outDir": "dist", "rootDir": "src", "strict": true, "noEmitOnError": true, "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, "exclude": ["**/*.spec.ts"] }
-
Add a
build
script topackage.json
to runtsc
."scripts": { "build": "rimraf dist && tsc", }
- We'll use
vitest
- a lightweight next generation drop-in replacement for jest