TypeScript不能正确转换类成员(其他类)

时间:2018-01-23 18:04:04

标签: javascript typescript

我遇到了一个我无法解决的问题。其余响应返回以下对象:

[{
  "username": "ljkkljk",
  "hotelRoles": [
    {
      "hotelId": "lkjkj",
      "role": {
        "code": "ROLE_USER",
        "name": "User administration"
      }
    },
    {
      "hotelId": "ljkklj",
      "role": {
        "code": "ROLE_USER",
        "name": "User administration"
      }
  }],
  "applications": [],
  "enabled": false,
  "title": null,
  "email": "lkj.lj@lkj.lkj",
  "firstName": "ljk",
  "lastName": "lkj",
  "middleName": null,
  "dateOfBirth": null,
  "telephone": null,
  "mobile": "+ljkl",
  "job": null,
  "department": null,
  "companyName": "ljkl",
  "employeeId": null,
  "taxId": null,
  "cashRegister": null,
  "address1": null,
  "address2": null,
  "townCity": null,
  "regionState": null,
  "postCode": null,
  "country": "ljkl",
  "website": null,
  "fullName": "jklkljkli"
}]

我尝试使用当前的TypeScript类结构映射它:

UserManagementData.ts

export class UsersManagementData {

  private username: string;
  private hotelRoles: HotelRole[];
  private title: string;
  private email: string;
  private firstName: string;
  private lastName: string;
  private middleName: string;
  private dateOfBirth: string;
  private telephone: string;
  private mobile: string;
  private job: string;
  private department: string;
  private companyName: string;
  private employeeId: string;
  private taxId: string;
  private cashRegister: string;
  private address1: string;
  private address2: string;
  private townCity: string;
  private regionState: string;
  private postCode: string;
  private country: string;
  private website: string;
  private fullName: string;

  constructor(usersManagementData: any = DEFAULT_VALUES) {
    Object.assign(this, usersManagementData);
  }

  get _username(): string {
    return this.username;
  }

  set _username(value: string) {
    this.username = value;
  }

  get _hotelRoles(): HotelRole[] {
    return this.hotelRoles;
  }

  set _hotelRoles(value: HotelRole[]) {
    this.hotelRoles = value;
  }

  get _title(): string {
    return this.title;
  }

  set _title(value: string) {
    this.title = value;
  }

  get _email(): string {
    return this.email;
  }

  set _email(value: string) {
    this.email = value;
  }

  get _firstName(): string {
    return this.firstName;
  }

  set _firstName(value: string) {
    this.firstName = value;
  }

  get _lastName(): string {
    return this.lastName;
  }

  set _lastName(value: string) {
    this.lastName = value;
  }

  get _middleName(): string {
    return this.middleName;
  }

  set _middleName(value: string) {
    this.middleName = value;
  }

  get _dateOfBirth(): string {
    return this.dateOfBirth;
  }

  set _dateOfBirth(value: string) {
    this.dateOfBirth = value;
  }

  get _telephone(): string {
    return this.telephone;
  }

  set _telephone(value: string) {
    this.telephone = value;
  }

  get _mobile(): string {
    return this.mobile;
  }

  set _mobile(value: string) {
    this.mobile = value;
  }

  get _job(): string {
    return this.job;
  }

  set _job(value: string) {
    this.job = value;
  }

  get _department(): string {
    return this.department;
  }

  set _department(value: string) {
    this.department = value;
  }

  get _companyName(): string {
    return this.companyName;
  }

  set _companyName(value: string) {
    this.companyName = value;
  }

  get _employeeId(): string {
    return this.employeeId;
  }

  set _employeeId(value: string) {
    this.employeeId = value;
  }

  get _taxId(): string {
    return this.taxId;
  }

  set _taxId(value: string) {
    this.taxId = value;
  }

  get _cashRegister(): string {
    return this.cashRegister;
  }

  set _cashRegister(value: string) {
    this.cashRegister = value;
  }

  get _address1(): string {
    return this.address1;
  }

  set _address1(value: string) {
    this.address1 = value;
  }

  get _address2(): string {
    return this.address2;
  }

  set _address2(value: string) {
    this.address2 = value;
  }

  get _townCity(): string {
    return this.townCity;
  }

  set _townCity(value: string) {
    this.townCity = value;
  }

  get _regionState(): string {
    return this.regionState;
  }

  set _regionState(value: string) {
    this.regionState = value;
  }

  get _postCode(): string {
    return this.postCode;
  }

  set _postCode(value: string) {
    this.postCode = value;
  }

  get _country(): string {
    return this.country;
  }

  set _country(value: string) {
    this.country = value;
  }

  get _website(): string {
    return this.website;
  }

  set _website(value: string) {
    this.website = value;
  }

  get _fullName(): string {
    return this.fullName;
  }

  set _fullName(value: string) {
    this.fullName = value;
  }
}

const DEFAULT_VALUES = {
  username: null,
  hotelRoles: null,
  title: null,
  email: null,
  firstName: null,
  lastName: null,
  middleName: null,
  dateOfBirth: null,
  telephone: null,
  mobile: null,
  job: null,
  department: null,
  companyName: null,
  employeeId: null,
  taxId: null,
  cashRegister: null,
  address1: null,
  address2: null,
  townCity: null,
  regionState: null,
  postCode: null,
  country: null,
  website: null,
  fullName: null
};

HotelRole.ts

export class HotelRole {

  private hotelId: string;
  private role: Role[];

  constructor(hotelRole: any = DEFAULT_VALUES) {
    Object.assign(this, hotelRole);
  }

  get _hotelId(): string {
    return this.hotelId;
  }

  set _hotelId(value: string) {
    this.hotelId = value;
  }

  get _role(): Role[] {
    return this.role;
  }

  set _role(value: Role[]) {
    this.role = value;
  }
}

const DEFAULT_VALUES = {
  hotelId: null,
  role: null
};

最后是 Role.ts

export class Role {

 private code: string;
 private name: string;

 constructor(role: any = DEFAULT_VALUES) {
   Object.assign(this, role);
 }

  get _code(): string {
    return this.code;
  }

  set _code(value: string) {
    this.code = value;
  }

  get _name(): string {
    return this.name;
  }

  set _name(value: string) {
    this.name = value;
  }
}

const DEFAULT_VALUES = {
  code: null,
  name: null
};

第一个父对象( UserManagementData.ts )正确映射,但 HotelRoles.ts Roles.ts 不会映射。它们映射的是简单的JS对象。

我做错了什么。我想我已经做过一百万次这样做了,从来没有遇到过这样的问题。任何帮助将很高兴。

1 个答案:

答案 0 :(得分:4)

问题是使用Object.assign不会将嵌套对象转换为类的实例,您需要手动调用构造函数:

export class UsersManagementData {

  constructor(usersManagementData: any) {
    Object.assign(this, usersManagementData);
    this.hotelRoles = usersManagementData.hotelRoles.map(hr => new HotelRole(hr));
  }
  ....
}

export class HotelRole {

  constructor(hotelRole: any) {
    Object.assign(this, hotelRole);
    this.role = new Role(hotelRole.role);
  }
}

注意

你的设计不是很面向JS / TS,如果我不得不猜测我会说你来自C#/ Java背景。我建议只要将类转换为接口,如果它们没有其他方法,(定义接口上的所有属性)并将JSON直接分配给接口类型的变量:

export interface UsersManagementData {
  username: string;
  hotelRoles: HotelRole[];
  title: string;
  email: string;
  firstName: string;
  lastName: string;
  middleName: string;
  dateOfBirth: string;
  telephone: string;
  mobile: string;
  job: string;
  department: string;
  companyName: string;
  employeeId: string;
  taxId: string;
  cashRegister: string;
  address1: string;
  address2: string;
  townCity: string;
  regionState: string;
  postCode: string;
  country: string;
  website: string;
  fullName: string;
}
export interface HotelRole {
  hotelId: string;
  role: Role[];
}

export interface Role {
  code: string;
  name: string;
}

let jsonResult: any; 
let umd : UsersManagementData = jsonResult;