打字稿-设置班级成员的默认值 [英] Typescript - set default value for class members

查看:187
本文介绍了打字稿-设置班级成员的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模型:

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

当我打电话到服务器(Angular 4,$ http)时,我通常会收到以下响应:

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

{
  name: string;
  email: string;
}

缺少age属性.

有什么方法可以使用我的模型并创建默认年龄(以防丢失)? 如果可能的话,我希望不必创建2个单独的模型.

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.

更新:

这是我对服务器的呼叫:

This is the call I making to the server:

results-manager.component.ts:

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:

results-manager.service.ts:

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

}

该请求有效,并且我收到响应,但是即使我定义了默认值(如@msanford的答案所建议),当我在组件中收到响应时,它们也会被删除.同样,如果我向模型添加构造函数(按照董建华的回答).

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?

推荐答案

您看到 覆盖 属性的原因是由于

The reason you're seeing overwriting properties is due to type erasure in TypeScript. TypeScript has no idea what types of objects are assigned to its variables during runtime. This would seem somewhat strange to you if you're coming not from a java / c# background.

最后,它只是JavaScript.而且JavaScript不执行严格的类型检查.

Because in the end , it's just JavaScript. And JavaScript doesn't enforce strict type checking.

为了确保您的配置文件对象始终具有age属性,您可以创建自己的对象,然后复制从响应中获取的值.这是将线格式转换为域对象映射时的常用方法.

In order to ensure that your profile objects always have an age property, you could create your own objects then copy over the values you get from the response. This is the usual approach when it comes to wire format to domain object mapping.

为此,首先创建您的域模型,在这种情况下,该类是具有默认age属性的Profile类.

To do this first create your domain model, a Profile class with a default age property in this case.

 export class Profile {
        constructor(
            public name: string,
            public email: string,
            public age: number = 0) { }
    }

然后将您的响应映​​射到域模型.

Then map your response to the domain model.

this.resultsManagerService.getResults(this.config.request.action, this.pagingAndSorting, this.args).subscribe(
  results => {
    let res = (results as Profile[]).map((profile) => new Profile(profile.name, profile.email, profile.age));
    this.results = this.results.concat(res);
 });

这篇关于打字稿-设置班级成员的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆