user3075338
user3075338

Reputation: 93

using interface to define a new type

I am using an interface to define a new type. Is this a correct approach? I do as following:

export interface IQuestionnaire {
  DataProcessingID: string;
  Qestions : [{
        SecurityControl :  string,
        securityGoal: string[],
          }]
}

Then, to instantiate from it I have to do this:

      IQuestions :  IQuestionnaire = {
    DataProcessingID: "",
     Qestions :[{
           SecurityControl :  "",
           securityGoal: [""]
               }]
  };

which actually create an empty object which is not what I want; but without this, it raises an error saying, for example, "Cannot read property 'Questions' of undefined". Is my approach wrong to define a new type?

====EDIT==== Here is what I did according to your comments:

export interface IQuestionnaire {
  DataProcessingID: string;
  Qestions :  Array<Iquestion>
}
interface Iquestion{
        SecurityControl :  string,
        securityGoal: string[],
}

and then I go:

 IQuestions ={} as IQuestionnaire;

Then I yet get this error: Can not read property 'push' of undefined . I don't want to initiate them where I define the interface, as it creates an empty object!

Upvotes: 0

Views: 85

Answers (3)

Korfoo
Korfoo

Reputation: 611

I prefer to do it like this:

interface Foo {
    bar: number
    bas: string
}

let foo = {} as Foo;
foo.bar = 123;
foo.bas = "Hello World";

You can read more about object initialization here or in this related stackoverflow question.

Combined with the other answers it could look like this:

export interface Question {
  securityControl: string,
  securityGoal: string[],
}

export interface Questionnaire {
  dataProcessingID: string;
  questions: Question[];
}

let questionaire = {} as Questionnaire;
questionaire.dataProcessingID = "12345";
questionaire.questions = [];

questionaire.questions.push({
    securityControl: "control"
    securityGoal: ["goal"]
});

Upvotes: 1

SiddAjmera
SiddAjmera

Reputation: 39432

Prefixing interface names with I is an anti-pattern in TypeScript as mentioned in this thread.

Also, since this interface will eventually act as a data model for a JSON Object, the properties in the interface should be in lowerCamelCase as a convention.

Consider creating your interface chain like this:

interface Question {
  securityControl: string,
  securityGoal: Array<string>,
}

export interface Questionnaire {
  dataProcessingID: string;
  questions: Array<Question>;
}

EDIT:

To initialize it, you'll do this:

questionnaire: Questionnaire = {
  dataProcessingID: '',
  questions: []
};

questionnaire.questions.push({
  securityControl: "",
  securityGoal: [""]
});

Upvotes: 2

wentjun
wentjun

Reputation: 42526

You should model your interface this way instead:

export interface IQuestionnaire {
  DataProcessingID: string;
  Qestion: Security[];
}

interface Security {
  SecurityControl: string;
  securityGoal: string[];
}

The reason being, Qestion is in fact an array of objects, and each object consists of the SecurityControl and securityGoal properties. Anyways, I believe you meant 'Question' rather than 'Qestion'.

And on your component.ts, you can simply initialise the object.

Upvotes: 2

Related Questions