30 May 2016

TypeScript: Using with NPM

Using TypeScript on OSX with NPM
  • Install: npm install -g typescript
  • Transpile:
    • tsc [.ts filename]
    • tsc --p ./
      NB: Cannot use --p (project/tsconfig location with a filename)
  • Install declarations e.g. to use JQuery:
    • Nuget: nuget install jquery.TypeScript.DefinitelyTyped
    • npm: Using Typings for DefinitelyTyped
      NB: Ensure you're in your typing's parent directory
      • npm install typings --save-dev
      • typings search --name jquery
        • take note of the 'source' column e.g. 'dt', you may need it
      • typings install dt~jquery --save --global
  • Use it, either:
    • declaration files: include /// <reference path="../typings/index.d.ts" />
    • or include in tsconfig: filesGlob": ["typings/index.d.ts"]
  • Include tsconfig.json e.g. below (see compiler options)
  • If you're using Gulp see also ntypescript
tsconfig.json:
{
    "compilerOptions": {
        "declaration": true,
        "module": "amd", <--- change to commonjs for server
        "noImplicitAny": true,
        "noLib": false,
        "outDir": "wwwroot/js/ts",
        "removeComments": true,
        "sourceMap": true,
        "target": "es5",
        "watch": true
    },
    "atom": {
        "formatOnSave": true
    },
    "filesGlob": [
        "./**/*.ts",
        "typings/index.d.ts",
        "!typings/**"
    ]
 }

No comments:

Post a Comment