user2294434
user2294434

Reputation: 163

Typescript : How to assign class and its members as one of the interface properties

in Type script I have an Interface and a class.

One of the interface's properties will be a class itself. How to assign the class and its members as one of the interface's properties ?

export interface ITest {
  _Id: string;
  _UserName: string;
   ***Criteria: Need to assign the class MyCriteria to this interface's property "Criteria"***
}

export class MyCriteria 
{
  _Name: string = "department";
  _Codes: string[]; // String array

  constructor(Name: string, Codes: string[]) {
    this._Name= Name;
   this._Codes= Codes;
  }
}

Upvotes: 0

Views: 30

Answers (1)

Todd Skelton
Todd Skelton

Reputation: 7239

You can just use MyCriteria as the type.

export interface ITest {
  _Id: string;
  _UserName: string;
   Criteria: MyCriteria
}

export class MyCriteria 
{
  _Name: string = "department";
  _Codes: string[]; // String array

  constructor(Name: string, Codes: string[]) {
    this._Name= Name;
   this._Codes= Codes;
  }
}
const test: ITest = {
    _Id: "",
    _UserName: "",
    Criteria: new MyCriteria("",[""])
}

test.Criteria._Codes = ["new codes"];

Upvotes: 1

Related Questions