首页 > 解决方案 > TypeScript:将结果转换为自定义类型不起作用

问题描述

我的 Person 类型定义如下

import moment from "moment";

export default class Person {
    constructor() {
        this.id = -1;
        this.name = "";
        this.dob = new Date();
        this.gender = "M";
        this.photo = "";
        this.salary = 0.0;
    }

   public id: number;
   public name: string;
   public dob: Date;
   public get dobString(): string{
        return moment(this.dob).toString();
   };
   public gender: string;
   public photo: string;
   public salary: number;
}

在上面的 Person 类型中,您可以看到我有一个只读属性dobString(),它几乎以字符串格式返回日期。

现在我有一个返回记录集合的 get 方法。我将集合投射到<Person[]>但结果不包括属性dobString()。您能否在我的代码下方验证并让我知道我缺少什么?

getAll (req: Request, res: Response, next: NextFunction) {
    var pageNumber = req.query.pageNumber;
    var pageSize = req.query.pageSize;

    db.query("CALL person_selectall(?, ?, @total); SELECT @total as TotalRecords;", [pageNumber, pageSize], (err: Error, rows: any[], fields: any) => {
        let result = new PageResult<Person>(pageSize, pageNumber, 0);

        if (!err) {
            result.IsSuccessful = true;
            result.TotalRecords = rows[2][0].TotalRecords;
            result.Data = <Person[]> rows[0];//result.Data is of type Person[]
            res.send(result);
        } else {
            result.IsSuccessful = false;
            result.TotalRecords = 0;
            result.ReasonForFailure = JSON.stringify(err);
            result.Data = [];
            res.send(result);
        }
    });
}

更新(2019 年 9 月 20 日)

现在,我已经使用 Map 方法实现了(见下文)。让我知道是否有更好的方法来做到这一点。

const personArray = rows[0].map((row: any) => {
                const person = new Person();
                person.Id = row.id;
                person.Name = row.name;
                person.Gender = row.gender;
                person.Dob = row.dob;
                person.DobString = moment(person.Dob).format(config.get("format.date"));
                person.Photo = row.photo;
                person.Salary = row.salary;
                person.CreatedDate = row.createddate;
                person.CreatedDateString = moment(person.CreatedDate).format(config.get("format.datetime"));
                person.ModifiedDate = row.modifieddate;
                person.ModifiedDateString = person.ModifiedDate === null ? null : moment(person.ModifiedDate).format(config.get("format.datetime"));
                return person;
            });
            result.Data = personArray;

谢谢,赫曼特。

标签: typescriptcasting

解决方案


result.Data = <Person[]> rows[0];

请记住,当 typescript 被转译时,所有类型注释都消失了,你只剩下result.Data = rows[0]. 该代码不会向该对象添加任何不存在的属性。

使用类型断言的目的<Person[]>不是对数据进行任何更改。目的是告诉打字稿“我比你更了解,所以不要在这里检查我的类型”。如果你真的比打字稿更了解,那没关系。但是,如果您断言该对象具有 dobString 函数,而实际上它没有,则打字稿只会信任您并且无法指出错误。


推荐阅读