Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better type inference for egg-mongoose #67

Open
Celend opened this issue Jul 28, 2020 · 0 comments
Open

Better type inference for egg-mongoose #67

Celend opened this issue Jul 28, 2020 · 0 comments

Comments

@Celend
Copy link

Celend commented Jul 28, 2020

Hi!, We're trying to use egg.js in production with mongoose, but we notice a problem about the typing for Mongoose with egg-ts-mongoose plugin.

Mongoose itself has the typing solution(separate interface declaration or use ts-mongoose), and the egg-ts-helper also generated a correct type file of model files(interface IModel), Awesome!
But when using the model in service by this.ctx.model, the model type is referred to the egg.Context.model in egg-ts-mongoose/index.d.ts(https://github.com/eggjs/egg-mongoose/blob/master/index.d.ts#L28), which type is [key: string]: mongoose.Model<any>.

So in the service, the model type is Model<any>, it's very stupid! and the solution also very easy, just override the type of context.model to IModel, by edit typing/index.d.ts as below:

import 'egg';

declare module 'egg' {
    // add below lines
    interface Context {
        model: IModel
    }
}

(Edit the typing/model/index.d.ts should has same result)

I think this job can not be done by egg-mongoose, because the interface IModel is a generated file.

I'm using WebStorm 2020.2, but I think the VSCode should has the same problem, because the context.model type refer to egg-mongoose seems a correct behavior or maybe I wrong :D

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant