mkdir -p typescript-hello-world/src
cd typescript-hello-world
yarn init
cat package.jsonnvm current > .nvmrcyarn add --dev typescript
yarn add --dev @types/node # all typings for node
yarn add --dev @tsconfig/node14 # recommend settings if we know what version of node we are targetting.These are all dev-dependencies.
Once our code has transpiled to JavaScript, these dependencies are not required to run.
cat << EOF > tsconfig.json
{
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"noEmitOnError": true // don't transpile the code if there's an error
},
"include": [
"./src/**/*.ts"
]
}
EOFWithin src, create index.ts with :
cat << EOF > src/index.ts
console.log("Hello World!");
EOFJust think of this as just rm -f but will work in all operating system
yarn add --dev rimraf{
// ---
"scripts": {
"build": "rimraf dist && tsc --project tsconfig.json",
"start": "node dist/index.js"
},
// ---
}yarn install # or just yarn
yarn build
yarn start