Angular Typescript Interface Default Value


Answer :

If you don't want to create an object and assign default values every time, use a class with default values for its properties instead. If you don't specify the values, defaults values will be used according to your property types.

export class MapMarker {     longitude: number = 0;     latitude: number = 0;     popupText: string = ''; } 

Then you can simply create an instance of your class and every field will be correctly initialized:

mapMarker: MapMarker = new MapMarker(); 

If you want to keep your interface, just implement it in the class:

export interface IMapMarker {     longitude: number;     latitude: number;     popupText: string; }  export class MapMarker implements IMapMarker {     longitude: number = 0;     latitude: number = 0;     popupText: string = ''; } 

If some of your properties are optional, use the optional operator for them:

export interface IMapMarker {     longitude?: number;     latitude?: number;     popupText?: string; } 

But then, you will always have to make sure the property is not null before using it.


Classes and interfaces are powerful structures that facilitate not just object-oriented programming but also type-checking in TypeScript. A class is a blueprint from which we can create objects that share the same configuration properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialization for them.

Since both of these structures define what an object looks like, both can be used in TypeScript to type our variables. The decision to use a class or an interface truly depends on our use case: type-checking only, implementation details (typically via creating a new instance), or even both! We can use classes for type-checking and the underlying implementation

if you are interested only in type-checking the responses Interface is a good choice Furthermore, an interface is a virtual structure that only exists within the context of TypeScript. The TypeScript compiler uses interfaces solely for type-checking purposes. Once your code is transpiled to its target language, it will be stripped from its interfaces - JavaScript isn’t typed, there’s no use for them there.

Since interfaces do not exist in runtime there is no runtime cost!

Regarding the issue you are facing:
Use this

mapMarker: MapMarker={longitude: 0,latitude: 0, popupText: ''}; 

There are at least two options:

1 Make every property of MapMarker optional

export interface MapMarker{    longitude?: number,    latitude?: number,    popupText?: string } 

2 Use class

export class MapMarker {     longitude: number = 0;     latitude: number = 0;     popupText: string = ''; } 

3 Just use a plain object with default value

mapMarker: MapMarker =  {     longitude: number = 0;     latitude: number = 0;     popupText: string = ''; } 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable