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

types: only apply BufferToBinary if type strictly equals Buffer | null | undefined #15072

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vkarpov15
Copy link
Collaborator

Fix #15057

Summary

The issue in #15057 is that BufferToBinary adds Binary to any type that extends undefined or extends null, which is not intentional. This PR now only applies the BufferToBinary transform if the type strictly equals Buffer | null | undefined. I also made a similar change to other transforms we use for calculating the JSON serialized type.

Examples

@vkarpov15 vkarpov15 added this to the 8.8.4 milestone Dec 3, 2024
Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR causes typegoose (in its current state) fail to build (types):

src/typeguards.ts:21:12 - error TS2677: A type predicate's type must be assignable to its parameter's type.
  Type 'Array<DocumentType<NonNullable<T>>>' is not assignable to type 'Array<Ref<T, S>>'.
    Type 'DocumentType<NonNullable<T>>' is not assignable to type 'Ref<T, S>'.
      Type 'DocumentType<NonNullable<T>>' is not assignable to type 'DocumentType<T, BeAnObject>'.
        Type 'DocumentType<NonNullable<T>>' is not assignable to type 'Document<unknown, BeAnObject, T>'.

21 ): docs is mongoose.Types.Array<DocumentType<NonNullable<T>>>;
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/typeguards.ts:27:102 - error TS2677: A type predicate's type must be assignable to its parameter's type.
  Type 'DocumentType<NonNullable<T>>[]' is not assignable to type 'Ref<T, S>[]'.
    Type 'DocumentType<NonNullable<T>>' is not assignable to type 'Ref<T, S>'.
      Type 'DocumentType<NonNullable<T>>' is not assignable to type 'DocumentType<T, BeAnObject>'.
        Type 'DocumentType<NonNullable<T>>' is not assignable to type 'Document<unknown, BeAnObject, T>'.

27 export function isDocumentArray<T, S extends RefType>(docs: Ref<T, S>[] | null | undefined): docs is DocumentType<NonNullable<T>>[];
                                                                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors in the same file, starting at: src/typeguards.ts:21

removing the NonNull<> from those 2 is locations seemingly fixes it, though it is quite weird to not be able to assert that T is not null or undefined anymore. (though it would be in-line with the isDocument definition from typegoose)

@vkarpov15 vkarpov15 modified the milestones: 8.8.4, 8.9.1 Dec 5, 2024
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

Successfully merging this pull request may close these issues.

Mongoose 8.7.2 breaks Union Types
2 participants