🔍 what is typescript

JavaScript and More

TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor.

  • TypeScript adds additional syntax to JavaScript to enable tighter integration with the editor. Catch errors early in the editor.

A Result You Can Trust

TypeScript code converts to JavaScript which runs anywhere JavaScript runs: In a browser, on Node.js or Deno and in your apps.

  • TypeScript code can be converted to JavaScript and run wherever JavaScript runs: in a browser, on Node.js or Deno, or in your application.

Safety at Scale

TypeScript understands JavaScript and uses type inference to give you great tooling without additional code.

  • TypeScript understands JavaScript and uses type inference to give you great tools that require no extra code.

Install the typescript

The installation

# npm
npm install typescript -D
# yarn 
yarn add typescript -D
Copy the code

Run the typescript compiler

NPX TSC or YARN TSCCopy the code

🔧 Tsconfig. Json configuration

The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.

  1. Manually createtsconfig.jsonContent is{}
  2. Automatically generatenpx tsc --init

The tsconfig.json file in the TypeScript project root directory specifies the root file and compiler options required to compile the project. TypeScript will include all.ts files in the directory and subdirectories that contain the tsconfig.json file as part of the compilation context.

{
  "compilerOptions": {},
  "files": [
    "src/hello.ts"
  ],
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}
Copy the code
  • compilerOptionsConfigure compilation options
  • filesExplicitly specify the file to compile
  • includeIncluded files
  • excludeExcluded file

Where path and file support wildcard characters, where/ * * *Represents all files under all paths

Compiling ts files

The compilation process is to convert ts files into JS files

  1. Compiling and formulating documentsnpx tsc hello.ts
  2. Compile from the configuration filenpx tsc --init npx tsc
  3. Listen for file changes to compile automaticallynpx tsc -w filePath