Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List equivalent in TypeScript

I am new in angular (TypeScript), I want to implement composition between two classes. How can i achieve that?

In c# if I have two classes like A and B then I implement composition like following

class A 
{
  int ID {get;set;}
  List<B> lstClassB {get;set;}
}

class B {
  int ID {get;set;}
}

How can I use this functionality in TypeScript like

export class A 
{
  ID :int;
  lstClassB :  ////what should I write here?
}

export class B {
  ID:int;
}
like image 821
Hunzla Ali Avatar asked Sep 16 '19 17:09

Hunzla Ali


3 Answers

It will be:

export class A 
{
   ID :number;
   lstClassB : Array<B> ;
}
like image 53
Adrita Sharma Avatar answered Sep 30 '22 09:09

Adrita Sharma


In Typescript you do B[] means type of B array

export class A {
    ID: number;
    list: B[];
}

To use setter and getter:

export class A {
   private _ID: number;
   set ID(value:number) {
       this._ID = value;
   }
   get ID():number {
       return this._ID;
   }
}
like image 33
kun Avatar answered Sep 30 '22 07:09

kun


You should use number and Array (or []) in your TypeScript definition:

export class A 
{
  ID: number;
  lstClassB: Array<B>;
}

or

export class A 
{
  ID: number;
  lstClassB: B[];
}
like image 37
Tom Faltesek Avatar answered Sep 30 '22 08:09

Tom Faltesek