typegoose icon indicating copy to clipboard operation
typegoose copied to clipboard

Change `Ref` type to transparently use `DocumentType`

Open hasezoey opened this issue 3 years ago • 2 comments

Describe what you need | want

Currently Ref<Class, Type> only passes the first generic (Class) directly through to the mongoose PopulatedDoc, which is not really great and also not the correct types

Do you have already an idea for the implementation?

change Ref from the following:

= mongoose.PopulatedDoc<PopulatedType, RawId>

to be

= mongoose.PopulatedDoc<DocumentType<PopulatedType>, RawId>

this change could potentially be breaking, so it is put off until a next major typegoose version

PS: this change would require changing isDocument typeguard to add | unknown to the first parameter as the allowed value to preserve being able to pass anything into it

hasezoey avatar Jun 29 '22 10:06 hasezoey

This change would also likely modify the Ref type directly by removing the | undefined possibility from the type directly

https://github.com/typegoose/typegoose/blob/2f001e4acb9f1ce4eca49d41a3b483e2184f1013/src/types.ts#L380

hasezoey avatar Jun 30 '22 11:06 hasezoey

also to maybe consider is changing the fallback case of when _id is not explicitly defined in a class for DocumentType to fallback to ObjectId instead of any this currently cannot be changed because typeguards expect ref but cannot narrow down the first parameter and so allow too much stuff

https://github.com/typegoose/typegoose/blob/2f001e4acb9f1ce4eca49d41a3b483e2184f1013/src/types.ts#L16

hasezoey avatar Jun 30 '22 11:06 hasezoey

:tada: This issue has been resolved in version 10.0.0-beta.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Dec 01 '22 19:12 github-actions[bot]

:tada: This issue has been resolved in version 10.0.0 :tada:

The release is available on:

This was added manually, because semantic-release hit a rate-limit and cannot be re-triggered

hasezoey avatar Dec 12 '22 15:12 hasezoey