Skip to the content.

Typescript references

Typescript features

Install Typescript

# Local installation
npm install typescript
npx tsc -v
# Global installation
sudo npm install typescript -g
tsc -v

Basic usage

# Convert .ts to .js
tsc src/example-1.ts
# Watch a directory...
tsc -w src/* --outDir dist/
tsc --watch src/* --outDir dist/ --target es5 # Default es3

Init TSC

cd typescript.demo.proj
mkdir {dist,src}

Create the package.json with some default settings:

npm init -y

Then update the scripts section in package.json:

"scripts": {
  "build": "tsc",
  "start": "npm run build -- -w"
},

Create the tsconfig.json with some default settings:

tsc --init

Then update the scripts section in tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": false,
    "module": "commonjs",
    "target": "es5",
  },
  "exclude": [
    "node_modules"
  ]
}

TypeScript Types

JavaScript have several native types, which also exist in TypeScript:

But TypeScript also adds:

Example:

Reference:

Functions

Example:

Classes

Example:

Reference:

Interfaces

Directory tree tree