0

I am trying to create variable in my component of certain type like below.

myrequest.model.ts
export class MyRequest {
    public endValue: string;
    public yearEnd: string;
}

In my component i import the above and do like below

myReqObj: MyRequest;

But if i try to do like

this.myReqObj.endValue = '23'

it trows a error like myReqObj is undefined. Am i doing the right way? What is the right way to do this.

2 Answers 2

3

Looks like you need to instantiate MyRequest. If you add a constructor, you can do this:

myReqObj: MyRequest = new MyRequest(null, null);
this.myReqObj.endValue = '23'
Sign up to request clarification or add additional context in comments.

Comments

2

You should be defining the value as

this.myReqObj ={
      endValue : '23'
}

Updated : Use interface for custom types like these.

export interface MyRequest {
    endValue: string;
    yearEnd: string;
}

3 Comments

Am i doing the things in right manner i.e. creating a class and trying to create a variable of that type or should i create a interface? Am following best practice?
you should be using the best practice (using interface)
Just a question. Why it should not a class?

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.