Setup, Commands & Config

Installation

  • Install typescript globally

    npm install -g typescript

  • Check version

    tsc -v

Note : tsc = Typescript compiler

Commands

  • Help to see params

    tsc -h

  • Generate javascript file from ts

    tsc filename.ts

Example

Consider test.ts

class StudentCls{
id;
constructor(){
this.id = 1;
}
getMarks(){
return 100;
}
}

tsc test.ts

Output js file at same location

var StudentCls = /** @class */ (function () {
function StudentCls() {
this.id = 1;
}
StudentCls.prototype.getMarks = function () {
return 100;
};
return StudentCls;
}());

Other params/ Options

  • '--module' to specify commonjs ..etc
  • '--watch' to actively watch the file for changes & compile to JS
  • '--target': JS version (default) ES3, ES2015..etc
  • '--outDir' : output directory of JS file
tsc --target ES2015 --outDir some_folder filename.ts --watch

Config

(tsconfig.json)

Docs

  • Default (tsc) compiler looks for tsconfig.json for settings else default settings like target = es3 , outDir = same folder ..etc

  • To generate config file

    tsc --init

  • Config file

    TS CONFIG

  • To run compiler at root, type following

    tsc

  • Config properties

    {
    "compilerOptions": {
    "module": "es2015",
    "noImplicitAny": true,
    /* Raise error if implied 'any' type, so explicitly types must be mentioned */
    "removeComments": true,
    "sourceMap": true,
    //generates map files (helpful for JS to TS debugging)
    "declaration": true
    //generates '.d.ts' file. (helpful for JS people to get intellisense..etc),
    "allowJs": true,
    /* Allow javascript files to be compiled. */
    "checkJs": true,
    /* Report errors in .js files. */
    },
    "files": [
    "core.ts",
    "sys.ts"
    ]
    }
    Instead of files ,can also specify following
    "include": [
    "src/**/*"
    ],
    "exclude": [
    "node_modules",
    "**/*.spec.ts"
    ]

Working with JS

Docs

  • Disable the type checks per line by adding the following comment

    // @ts-ignore

  • To disable type checks on an entire file, you can use

    // @ts-nocheck

VSCODE Docs for JS

@ts-nocheck in TypeScript (3.7) files too