In this blog post we are going to cover a basic setup for working with Typescript and Typings in a Node environment. This is the starting point of many other post on this blog.
Setting Up the Environment
The first step in the process is to create a working directory. We are going to use the generic name “project-name” but feel free to use a more meaningful name for your own project.
$ mkdir project-name
$ cd project-name
The next step is to create a “package.json” file and install our main dependencies, “typescript” and “typings”.
$ npm init -f
$ npm install typescript typings --save
We can now add our own commands to “package.json” to work with the local version of the libraries just installed.
{
"name": "project-name",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"tsc": "tsc",
"tsc:init": "tsc --init",
"tsc:watch": "tsc -w",
"typings": "typings",
"typings:install": "typings install",
"postinstall": "typings install"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"typescript": "^1.8.10",
"typings": "^1.3.0"
}
}
We also need to create the file “tsconfig.json” to instruct typescript how to compile our files using one of our custom commands.
$ npm run tsc:init
Finally, because we are going to be using the latest javascript features in our code, we need to install the typescript definitions for ES6.
$ npm run typings:install -- dt~core-js --save --global
We are ready now to start creating typescript files. To compile our typescript files on the fly, we can use one of our custom commands.
$ npm run tsc:watch
Now, every time a new typescript file is created or updated, the compiler will do its work and create the corresponding javascript file automatically.
The source code for this setup can be found here.
Thanks!.