0

I'm creating a model in angular, any element has its own type:

export class myModule {
 constructor(
   public name: string,
   public condition: boolean,
   public age: number){}
}

the problem is the type of data is not checked, I can create:

mymodule: myModule = new myModule(3,'hi!', true)

and it works, in name I have 3, in condition, 'hi!' and in age true.

I guess it should be a way to control it, not checking any data individually, isn't?

2
  • you should get a type error Commented Sep 25, 2018 at 10:42
  • can you tell us how you compile it and give us the output of the compiler? Commented Sep 25, 2018 at 10:47

3 Answers 3

0

Declare your properties outside the constructor. The constructors purpose is to initialize the properties with values. Example:

export class myModle {
    public name: string,
    public condition: boolean,
    public age: number
}
Sign up to request clarification or add additional context in comments.

Comments

0

Are you aware that you define myModle and then use the type below myModule?

1 Comment

sorry, spelling mistake in my post. Updated
-2

I think your answer lies with some further knowledge of JavaScript typing, rather than Angular. Have a look at this: How to use typed variables in javascript?

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.