typewriter

typescript basics

Add typescript to a project

#install dependencies
npm install @types/node typescript --save-dev

# or for yarn
yarn add @types/node typescript--dev

#create config
npx tsc --init --rootDir src --outDir build \
--esModuleInterop --resolveJsonModule --lib es6 \
--module commonjs --allowJs true --noImplicitAny true

#run script
npx ts-node hash.ts

If you have multiple ts modules

#install dependencies
npm install @types/node typescript --save-dev

# or for yarn
yarn add @types/node typescript--dev

#create config
npx tsc --init --rootDir src --outDir dist \
--esModuleInterop --resolveJsonModule --lib es6 \
--module es2020 --allowJs true --noImplicitAny true  --moduleResolution node --allowSyntheticDefaultImports

#run
node --trace-warnings --loader ts-node/esm ./src/main.ts

#or
NODE_OPTIONS='--no-warnings --loader ts-node/esm' nodemon --watch 'src/**/*' -e ts,tsx --exec node --trace-warnings --loader ts-node/esm ./src/main.ts

More info can be found here

Debug in WebStorm

#add to node parameters sections on node.js runtime config

--trace-warnings --require ts-node/register  --inspect
Example runtime config
Example runtime config

Or with multiple modules

--trace-warnings --loader ts-node/esm --inspect
screenshot
Example runtime config

More info can be found here

Useful Links

https://khalilstemmler.com/blogs/typescript/node-starter-project/