Typegoose: Weird type error at create function

Created on 18 Aug 2020  Â·  8Comments  Â·  Source: typegoose/typegoose

Versions

  • typegoose: 7.3.2
  • mongoose: 5.10.0
  • @types/mongoose: 5.7.36

My project (with commit)

https://github.com/huming2207/datasheep-api/tree/05648be0dec997607d9ebdfa8a59757fce422512

Question

Hi all,

I've started using this library a week ago. Everything is fine except there is an annoying type error at .create() function, for example, here: https://github.com/huming2207/datasheep-api/blob/05648be0dec997607d9ebdfa8a59757fce422512/src/handlers/ListHandler.ts#L61

If I change the create call from:

    const list = await ListModel.create({
        title,
        color,
        owner: user,
        project: project._id,
    });

to:

    const list = await ListModel.create({
        title,
        color,
        owner: user,
        project: project,
    });

then I will get this error:

image

Here's a full call stack log from tsc:

TSError: ⨯ Unable to compile TypeScript:
src/handlers/ListHandler.ts(61,9): error TS2769: No overload matches this call.
  The last overload gave the following error.
    Type 'DocumentType<Project>' is not assignable to type 'string | ObjectId | { _id: string | ObjectId; __v?: string | number | undefined; __t?: string | number | undefined; owner: string | ObjectId | { _id: string | ObjectId; ... 4 more ...; __t?: string | ... 1 more ... | undefined; }; ... 5 more ...; lists?: (ObjectId | ... 1 more ... | undefined)[] | undefined; }'.
      Type 'DocumentType<Project>' is not assignable to type '{ _id: string | ObjectId; __v?: string | number | undefined; __t?: string | number | undefined; owner: string | ObjectId | { _id: string | ObjectId; ... 4 more ...; __t?: string | ... 1 more ... | undefined; }; ... 5 more ...; lists?: (ObjectId | ... 1 more ... | undefined)[] | undefined; }'.
        Types of property 'owner' are incompatible.
          Type 'Ref<User, ObjectId | undefined>' is not assignable to type 'string | ObjectId | { _id: string | ObjectId; __v?: string | number | undefined; username: string; password: string; email: string; __t?: string | number | undefined; }'.
            Type 'undefined' is not assignable to type 'string | ObjectId | { _id: string | ObjectId; __v?: string | number | undefined; username: string; password: string; email: string; __t?: string | number | undefined; }'.

    at createTSError (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:434:12)
    at reportTSError (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:438:19)
    at getOutput (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:578:36)
    at Object.compile (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:775:32)
    at Module.m._compile (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:858:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Object.require.extensions.<computed> [as .ts] (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/home/hu/Projects/datasheep-api/src/handlers/ProtectedRequests.ts:3:1)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Module.m._compile (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Object.require.extensions.<computed> [as .ts] (/home/hu/Projects/datasheep-api/node_modules/ts-node/src/index.ts:861:12)

I'm guessing the project from findById() is DocumentType<Project>, where in the create() it needs just Project itself. Or maybe I misunderstood something else.

Could you please provide some suggestions? Thanks!

Regards,
Jackson

question

All 8 comments

Yep, getting the same issue here.
Screen Shot 2020-08-19 at 10 43 48 am

Please fix :) Thanks

Sorry guys, I've got some typos in my original post. I've just fixed it up. Please re-read it. Thanks.

okay no problem

If I set @types/mongoose to 5.7.20, and re-do npm i, then it works.

Seems like it's an upstream issue...

@huming2207 @types/mongoose before 5.7.22 will suppress all type checking because it was untyped (aka defined as .create(doc: any))

I think the problem here is in the Ref<> type definition from typegoose. I ran into something similar before but I don't remember the details.

In particular it shows 'Ref<User, ObjectId | undefined>' in your stacktrace but I don't think undefined should be there. If you can create a minimal repro I could take a look.

Hi @andreialecu ,

Thanks for your reply. The Ref<> is at here: https://github.com/huming2207/datasheep-api/blob/05648be0dec997607d9ebdfa8a59757fce422512/src/models/ListModel.ts#L22

I've tried making it to be: Ref<Project> or Ref<Project, Types.ObjectId> or Ref<Project, string> but neither of them works.

Jackson

offically tested (from tests in ci) is @types/[email protected], and unoffically (testing repo) is @types/[email protected]

yes, project: project // project being the document is supported by mongoose, but gives not-easy-to-fix type errors, so i always recommend using project: project._id

Hi @hasezoey ,

Alright, we will use _id instead. Thanks.

Jackson

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerolloz picture kerolloz  Â·  6Comments

Negan1911 picture Negan1911  Â·  10Comments

akash-rajput picture akash-rajput  Â·  4Comments

kerolloz picture kerolloz  Â·  6Comments

mohamedGamalAbuGalala picture mohamedGamalAbuGalala  Â·  5Comments