0

My typescript class - with constructor - looks like

export class Message {

    constructor(
        public id: number = 0,
        public message: string = '',
    public fact: string = ''
    ) {
    }
}

I'm able to create a new model of this class like

  model = new Message(0, '', '');

How would I create a constructor for a new class with a nested inner class such as

export class Inventory {
  inventoryKey: string;
  inventory: {
        createBy: string;
        component: string;
        url: string;
        securityCredentials: string;
        description: string;
        createDt: string;
      }

}

? Not sure what the syntax for the constructor would look like. Thanks for any advice.

1 Answer 1

1

How would I create a constructor for a new class with a nested inner class such as

I do not see any mention of the desired nesting. I suspect you are calling the property inventory a class. Here is one way you can have a constructor for Inventory

export class Inventory {
  inventoryKey: string;
  inventory: {
        createBy: string;
        component: string;
        url: string;
        securityCredentials: string;
        description: string;
        createDt: string;
      }
   constructor() {} // EASY
}

You can easily take the property inventory as a constructor argument :

export class Inventory {
    inventoryKey: string;
    constructor(public inventory: {
        createBy: string;
        component: string;
        url: string;
        securityCredentials: string;
        description: string;
        createDt: string;
    }) {

    } // EASY
}

More

Best learnt by reading the manual ;) https://basarat.gitbooks.io/typescript/content/docs/classes.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.