Fandom

Wikia Developers Wiki

TypeScript

847pages on
this wiki
Add New Page
Talk0 Share

Let's explain what TypeScript is base on examples not some academic definitions. Let's take step by step.

Basic types

  • Boolean
  • Number
  • String
  • Array
  • Enum
  • Any
  • Void

All types are explained here

Typed vars

var name: string = "Joe"

Variable's "name" type is set to string. Trying to call something like:

function sum(num: number): number {
    return num + num;
}

square(name);

would cause an error.

In TypeScript there is a type called any. Following function would sum/concat/merge any value which is passed as an argument:

function sum(val: any): any {
    return val + val;
}

Interfaces

Interfaces allows to define shape on an object.

interface Person {
  name: string;
  lastname: string;
  age: number;
  nickname?: string

}

And this is the definition of an object which uses this interface:

var lily: Person = {
    name: "Lily",
    lastname: "Doe",
    age: 27
}

Note that nickname is not defined here. ? next to "nickname" means that this property is optional. Trying to do something like:

lily.occupation = "actress";

would raise an error

Class definition

TypeScript allows to define classes which looks pretty much the same as ES6 classes. Right now the main difference is that TS allows to define class properties and ES6 doesn't support it.

class MyClass {
      params: MyClassParams;
 
 constructor(params: MyClassParams) {
      this.params = params;
 }
 
 getArticle(): Promise<ArticleResponse> {
      return getUrl(this.params.url);
 }
}

Useful resources

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.