Web and Node environment configuration TypeScript

Node

  1. install
npm init 
npm install -g typescript
tsc -init
  1. Add outDir and include to the file tsconfig.json file
{
  "compilerOptions": {
   .....
   "outDir": "./dist",
  },
  "include": [
    "src/**/*",
  ]
}
  1. Create a new index.ts the src directory, and write the
console.log(666);

4.Directory to execute tsc to generate dist files, and then execute node ./dist/index.js

tsc
node ./dist/index.js

export 666, stage comletion

  1. Configure automatic restart
npm install --save nodemon

Create a new nodemon.json in the root directory,copy the content

{
    "ignore": [
        "**/*.test.ts",
        "**/*.spec.ts",
        ".git",
        "node_modules"
    ],
    "watch": [
        "src",
    ],
    "exec": "npm start",
    "ext": "ts"
}
  1. package.json, Add start two lines of command
"scripts": {
    "start": "tsc && NODE_ENV=development node ./dist/index.js ",
    "dev": "./node_modules/nodemon/bin/nodemon.js"
  },
  1. Start, complete full configuration
npm run dev

Web

  • Configuring your own webpack is very troblesome ,With react cli。
create-react-app my-app --scripts-version=react-scripts-ts
cd my-app
npm run start  

Ending

the environment configuration for TypeScript write games is complete。

点赞