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;
}
It will be:
export class A
{
ID :number;
lstClassB : Array<B> ;
}
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;
}
}
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[];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With