如何处理猫鼬中也使用过的打字稿界面

时间:2020-06-09 11:27:00

标签: typescript

我正在将typescript与猫鼬一起使用,一切正常,但是现在我想在没有完整参考文献但只有一个id的地方使用def类型,例如,这是我的模型:

IUser:

export interface IUser {
    email: string,
    firstName: string,
    lastName: string,
}

IWorkLog:

export interface IWorkLog {
    author: IUser,
    body: string,
}

WorkLogSchema:

const WorkLogSchema: Schema = new Schema(
    {
        author: { type: String, ref: 'User', required: true },
        body: { type: String, required: true },
        timeSpent: String,
        timeSpentSeconds: Number
    }
);

const WorkLog = mongoose.model<IWorkLog>("WorkLog", WorkLogSchema);
export default WorkLog;

到目前为止,一切正常(我认为,我是TS的新手),现在在服务中,我得到了一些参数来创建工作日志,我想告诉它类型,但是author属性我得到的不是完整的对象,而只是id,那么如何告诉打字稿使用正确的定义?

    // the if I use IUser instead of 'any' here, it would expect a full 
    // user object, but my author property is just an id string, so how 
    // do I solve this issue?

    async create(args: any): Promise<WorkLog> {
        // ...create document
    }

0 个答案:

没有答案