Getting Started with TypeScript
Installing TypeScript
npm install -g typescript
Using tsc
In your code editor create a file logger.ts
with the following content:
function log(message) {
console.log(message);
}
log('Hello, world!');
Now you can use a command line to compile your source code to ES5 with tsc
tool and run it with node.js
:
tsc logger.ts
node logger.js
TypeScript compiler takes logger.ts
file, processes it and produces a JavaScript output to logger.js
.
At this point, the .js
file is ready to be used with an HTML page or executed by node.js.
You should see the following output in the command line:
Hello, world!
Now let's see how type validation works.
Add string
type annotation for the log
function and call it with a number
.
function log(message: string) {
console.log(message);
}
log(0);
If you compile logger.ts
once again tsc
should produce an error:
tsc logger.ts
> logger.ts(5,5): error TS2345: Argument of type '0' is not assignable to parameter
of type 'string'.
Typings
Linting
TSLint
TSLint checks your TypeScript code for readability, maintainability, and functionality errors.
npm install -g tslint