it:ad:typescript:howto:learn:home

IT:AD:TypeScript:HowTo:Learn

Summary

The full documentation is available here: IT:AD:TypeScript:HowTo:Documentation

But you probably don't need all of it to get going.

Variables are now typed as any, boolean, string, number or even an interface/class:

var a : any;
var b : boolean;
var s : string;
var n : number;
var p : IPerson;  // Notice this (see later)

Nicely, there is now also the enum type:

enum Sex {"Male", "Female"};
var c = Sex.M;
var n = 3;
var s = "neat";

Arrays can be typed as well:

var al = any[]{1,"2",true,"4",5};
var bl = boolean[]{true,false};
var nl = number[]{1,2,3,4,5};
var sl = string[]{"1","2","3","4","5"};
var pl = IPerson[]{p1,p2,p3, new Person("Joe")};

Functions now specify their return type:

function someFunctionName1(n:number, s:string,a:any):number {return 3;}
//but you can also use lamba expressions:
var someFunctionName2 = (n:number) : number => {return i*i;}
var someFunctionName3 = (n:number) : void => {console.log (i*i);}

Typescript also introduces interfaces:

interface iPerson {
  // Properties:
  name : string;
  private hairColor : string;
  
  // Methods:
  sayHi():void;
}

Which can be implemented with classes:

class Person implements iPerson {
  public constructor (name:string){
    this.name = name;
  }
  function sayHi () {console.log("Hello " + name);
}

class inheritance is then possible using the extends keyword:

class Student extends Person {
  isCool :boolean;
  constructor(name:string,isCool:boolean = false){
    //call the base constructor:
    super(name);
    this.isCool = isCool;
  }
}

There's even the concept of generic types, just like in C#:

interface IFoo<T> {
  bar T;
}
class Foo implements IFoo<number> {
  bar : number;
}

  • /home/skysigal/public_html/data/pages/it/ad/typescript/howto/learn/home.txt
  • Last modified: 2023/11/04 02:31
  • by 127.0.0.1