TypeScript: Difference between revisions

From David's Wiki
(Created page with "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....")
 
Line 31: Line 31:


==Compilation==
==Compilation==
* Install TypeScript
You will need to compile your typescript before you can run it on Node.js or in the browser. 
*:<pre>npm install -g typescript</pre>
There are many ways to compile your typescript. 
* Compile your typescript file
For Node programs
*:<pre>tsc greeter.js</pre>
 
===tsc===
# Install TypeScript
#:<pre>npm install -g typescript</pre>
# Compile your typescript file
#:<pre>tsc greeter.js</pre>
 
===webpack===
See [https://webpack.js.org/guides/typescript/ Webpack: TypeScript]
 
===babel===
See [https://babeljs.io/docs/en/babel-preset-typescript Babel preset typescript]


==Resources==
==Resources==
* [https://www.typescriptlang.org/docs/handbook/intro.html The TypeScript Handbook]
* [https://www.typescriptlang.org/docs/handbook/intro.html The TypeScript Handbook]

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

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

tsc

  1. Install TypeScript
    npm install -g typescript
  2. Compile your typescript file
    tsc greeter.js

webpack

See Webpack: TypeScript

babel

See Babel preset typescript

Resources