TypeScript: Difference between revisions
Line 33: | Line 33: | ||
You will need to compile your typescript before you can run it on Node.js or in the browser. | You will need to compile your typescript before you can run it on Node.js or in the browser. | ||
There are many ways to compile your typescript. | There are many ways to compile your typescript. | ||
For Node programs | For Node programs, you can just use tsc or babel. | ||
For browser applications, webpack can compile typescript. | |||
===tsc=== | ===tsc=== |
Revision as of 02:13, 15 August 2020
TypeScript is a typed version of JavaScript. It claims to be a superset of JavaScript.
Syntax
Here we focus on the syntax differences between TypeScript and JavaScript.
Classes
In TypeScript, you should declare properties above the constructor like so:
class Greeter {
greeting: string;
constructor(message: string) {
this.greeting = message;
}
greet() {
return "Hello, " + this.greeting;
}
}
Interfaces
Unlike JavaScript, TypeScript comes with support for interfaces:
interface LabeledValue {
label: string;
}
Compilation
You will need to compile your typescript before you can run it on Node.js or in the browser.
There are many ways to compile your typescript.
For Node programs, you can just use tsc or babel.
For browser applications, webpack can compile typescript.
tsc
- Install TypeScript
npm install -g typescript
- Compile your typescript file
tsc greeter.js