nodejs typescript code example

Example 1: add typescript in node

// Follow the below steps and you good to go easily

// create a new folder and open a terminal on that folder and type below commands
	npm init-y

	yarn add -D @types/node typescript ts-node

	npx tsconfig.json                 

// open package.json and add in  scripts
    "start": "ts-node src/index.ts",
     
//	create new folder src/index.ts ..... open - index.ts
     console.log("Hello TypeScript");

//// And type below cmd for start....
	yarn start
    
//// DONE!!!!!!!

Example 2: install typescript in node project

//Author:Mohammad Arman Khan
//Command to install typeScript in node(express)Project
npm i -D @types/node typescript ts-node

Example 3: npm typescript

npm install -g typescript

Example 4: typescript tutorial nodejs

npm install express @types/express

Example 5: npm typescript package

{
   "compilerOptions": {
       "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
       "module": "commonjs",
       "allowJs": true, /* Allow javascript files to be compiled. */
       "strict": true, /* Enable all strict type-checking options. */
       "outDir": "./lib", /* Redirect output structure to the directory. */
       "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
   },
   "include": ["src"],
   "exclude": ["node_modules"]
}

Example 6: nodejs typescript

try this config for nodejs + typescript using babel

https://pastebin.com/TR4BXPqL