TypeScript: Difference between revisions

From David's Wiki
Line 26: Line 26:


TypeScript does not support overloading in the traditional sense, with multiple implementations.   
TypeScript does not support overloading in the traditional sense, with multiple implementations.   
However you can have multiple function definitions similar to prototypes.
However you can have multiple function declarations similar to prototypes.


===Interfaces===
===Interfaces===

Revision as of 20:32, 9 February 2021

TypeScript is a typed version of JavaScript. It claims to be a superset of JavaScript but most JS code will need modifications to compile properly as TS code.
The syntax is very similar to Kotlin.

Syntax

Here we focus on the syntax differences between TypeScript and JavaScript. See JavaScript for JS syntax.

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;
  }
}

Functions

Overloads

See Overloads

TypeScript does not support overloading in the traditional sense, with multiple implementations.
However you can have multiple function declarations similar to prototypes.

Interfaces

Interfaces

Unlike JavaScript, TypeScript comes with support for interfaces:

interface LabeledValue {
  label: string;
}

Generics

Generics

Type Guards

Advanced Types

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

  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