0

I am confused about variable declartion inside angular component and want to know the purpose why we are declaring it in below way

 export class AppComponent {
  serverElements = [];
  newServerName = '';
  newServerContent = '';
}

and why we are not using type declartion here of typescript.

1 Answer 1

2

...want to know the purpose why we are declaring it in below way...

Those are property declarations with initializers. The resulting properties will exist on instances of the class (and will be public, just as though they had public in front of them).

...and why we are not using type declartion here of typescript...

TypeScript will infer the types from the initialization values (more in the documentation). If you're initializing something when you're defining it, you often don't need to explicitly provide the type.

That said, there should be a type for serverElements since it will end up being never[] (in an up-to-date version of TypeScript), which probably isn't what the author wanted. For instance, if it was supposed to be an array of ServerElement instances:

export class AppComponent {
  serverElements: ServerElement[] = [];
  newServerName = '';
  newServerContent = '';
}

newServerName and newServerContent are both inferred as string, which is reasonable.

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.