Ever wondered why we declare properties in this specific way?
These declarations are for initializing properties. These properties will be available on class instances and will be public by default, similar to if they were declared with the keyword public
.
Have you noticed the absence of type declarations in TypeScript here?
TypeScript automatically infers types from initialization values (more details can be found in the documentation). If a value is initialized during declaration, specifying the type explicitly is often unnecessary.
However, it's important to provide a type for serverElements
to prevent it from defaulting to never[]
. For example, if it should be an array of ServerElement
instances:
export class AppComponent {
serverElements: ServerElement[] = [];
newServerName = '';
newServerContent = '';
}
newServerName
and newServerContent
are correctly inferred as strings.