Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - set default value for class members

Tags:

I have a simple model:

export class Profile extends ServerData {
  name: string;
  email: string;
  age: number;
}

When I am make a call to the server (Angular 4, $http) I often get the following response:

{
  name: string;
  email: string;
}

The age property is missing.

Is there any way to use my model and create a default age in case it is missing? I would prefer not to have to create 2 separate models if possible.

I don't want to create the age as an optional property - I need it, even if it is just with an incorrect default.

UPDATE:

This is the call I making to the server:

results-manager.component.ts:

this.resultsManagerService.getResults(this.config.request.action, this.pagingAndSorting, this.args).subscribe(
  results => {
    this.results = this.results.concat(results as Profile[]);

results-manager.service.ts:

getResults(action: string, pagingAndSorting: PagingAndSorting, args: string[]): Observable<Profile[]> {
return this.apiService.callServer(
  pagingAndSorting,
  action,
  ...args );

}

The request works and I receive the response, but even if I define the default values (as suggested by @msanford's answer) they get removed when I receive the response back in the component. Likewise if i add a constructor to the model (as per Mike Tung's answer).

It seems like the backend response is completely overwriting the model - not just assigning the values.

How can I get it to just assign the values to the model and not remove the values it does not return?

like image 841
joshua miller Avatar asked Mar 22 '18 17:03

joshua miller


People also ask

How do I provide a default value in TypeScript?

To set a default value for a function parameter, use an equal sign right after the parameter name, e.g. function multiply(num: number, by = 10) {} . If a value for the parameter is not provided, the argument will be replaced with the default value. Copied!

What is the default access modifier for members of a class in TypeScript?

Access modifiers control the accessibility of the members of a class. TypeScript has two access modifiers – public and private. By default the members are public but you can explicitly add a public or private modifier to them.

Can we set default value in interface TypeScript?

In TypeScript, interfaces represent the shape of an object. They support many different features like optional parameters but unfortunately do not support setting up default values.

How do I give a default value to optional parameter TypeScript?

Use default parameter syntax parameter:=defaultValue if you want to set the default initialized value for the parameter. Default parameters are optional. To use the default initialized value of a parameter, you omit the argument when calling the function or pass the undefined into the function.


1 Answers

Yes, easily, and you don't need to add a class constructor.

export class Profile extends ServerData {
  name: string;
  email: string;
  age: number = 0;
}

The ability to define default values is one of the main things that differentiates a class from an interface.

For this to work you need to call new Profile() somewhere in your code, otherwise a class instance won't be created and you won't have defaults set, because the above TypeScript will compile to the following JavaScript:

var Profile = /** @class */ (function () {
    function Profile() {
        this.age = 0;
    }
    return Profile;
}());

So just using it for type assertion at compile-time isn't sufficient to set a default at run-time.

See it in action in the TypeScript Playground.

like image 188
msanford Avatar answered Oct 15 '22 07:10

msanford