- Install TypeScript compiler
$ sudo npm install -g typescript
$ which tsc
/usr/bin/tsc
$ tsc --version
Version 5.3.2
- install tx-node
$ sudo npm install -g ts-node
[alexlai@orgpi58G tsc-proj]$ which ts-node
/usr/bin/ts-node
$ ts-node
> console.log("Hello, world!")
Hello, world!
undefined
> function greet(name: string): string {
... return `Hello, ${name}!`;
... }
undefined
>
undefined
> const result = greet("John");
undefined
> console.log(result);
Hello, John!
undefined
>
- TypeScript is JavaScript with syntax for types
- The TypeScript Handbook
- TypeScript for JavaScript Programmers
TypeScript offers all of JavaScript’s features, and an additional layer on top of these: TypeScript’s type system.
JavaScript provides language primitives like string and number, but it doesn’t check that you’ve consistently assigned these. TypeScript does.
This means that your existing working JavaScript code is also TypeScript code. The main benefit of TypeScript is that it can highlight unexpected behavior in your code, lowering the chance of bugs.
TypeScript is built and maintained by Microsoft. Microsoft's strapline for TypeScript is JavaScript that scales because it helps write large JavaScript based programs.
TypeScript is a superset of JavaScript. Any feature in JavaScript is available in TypeScript. TypeScript adds a powerful type system to the JavaScript we already know and love. This type system enables code editors to provide code refactoring and navigation features along with type checking. TypeScript helps a JavaScript codebase become more readable and easier to maintain by adding a static type system.
TypeScript doesn't directly execute in the browser. Like JSX, it needs to be converted to JavaScript first. TypeScript has a compiler that can do this after type checking the code. The Babel compiler can convert TypeScript code to JavaScript as well.
TypeScript is not limited to browser-based apps. It can be used on the backend as well to write nodejs based apps.