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

7.2.2 broke some Array types when used in typeguards #13482

Closed
2 tasks done
hasezoey opened this issue Jun 5, 2023 · 0 comments · Fixed by #13494
Closed
2 tasks done

7.2.2 broke some Array types when used in typeguards #13482

hasezoey opened this issue Jun 5, 2023 · 0 comments · Fixed by #13494
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@hasezoey
Copy link
Collaborator

hasezoey commented Jun 5, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.2.2

Node.js version

20.2.0

MongoDB server version

unrelated

Typescript version (if applicable)

4.9.5

Description

mongoose 7.2.2 (via #13446) broke some types that are used in typegoose, but only typeguards that use mongoose.Types.Array

fixes would be to either remove the following line, or remove the <T> from the line

toObject(options?: ToObjectOptions<T>): any;

both resolve the issue, i dont understand why typescript complains, but the mentioned solution(s) above resolve it for this case, but i dont know if this would break some types tests

full error (from the reproduce script):

src/test.ts:11:12 - error TS2677: A type predicate's type must be assignable to its parameter's type.
  Type 'Array<S>' is not assignable to type 'Array<PopulatedDoc<T, S>>'.
    Types of property 'toObject' are incompatible.
      Type '{ (options?: ToObjectOptions<S> | undefined): any; <T>(options?: ToObjectOptions<T> | undefined): T; }' is not assignable to type '{ (options?: ToObjectOptions<PopulatedDoc<T, S>> | undefined): any; <T>(options?: ToObjectOptions<T> | undefined): T; }'.
        Types of parameters 'options' and 'options' are incompatible.
          Type 'ToObjectOptions<PopulatedDoc<T, S>> | undefined' is not assignable to type 'ToObjectOptions<S> | undefined'.
            Type 'ToObjectOptions<PopulatedDoc<T, S>>' is not assignable to type 'ToObjectOptions<S>'.
              Types of property 'transform' are incompatible.
                Type 'boolean | ((doc: PopulatedDoc<T, S>, ret: Record<string, any>, options: ToObjectOptions<PopulatedDoc<T, S>>) => any) | undefined' is not assignable to type 'boolean | ((doc: S, ret: Record<string, any>, options: ToObjectOptions<S>) => any) | undefined'.
                  Type '(doc: PopulatedDoc<T, S>, ret: Record<string, any>, options: ToObjectOptions<PopulatedDoc<T, S>>) => any' is not assignable to type 'boolean | ((doc: S, ret: Record<string, any>, options: ToObjectOptions<S>) => any) | undefined'.
                    Type '(doc: PopulatedDoc<T, S>, ret: Record<string, any>, options: ToObjectOptions<PopulatedDoc<T, S>>) => any' is not assignable to type '(doc: S, ret: Record<string, any>, options: ToObjectOptions<S>) => any'.
                      Types of parameters 'options' and 'options' are incompatible.
                        Type 'ToObjectOptions<S>' is not assignable to type 'ToObjectOptions<PopulatedDoc<T, S>>'.
                          Types of property 'transform' are incompatible.
                            Type 'boolean | ((doc: S, ret: Record<string, any>, options: ToObjectOptions<S>) => any) | undefined' is not assignable to type 'boolean | ((doc: PopulatedDoc<T, S>, ret: Record<string, any>, options: ToObjectOptions<PopulatedDoc<T, S>>) => any) | undefined'.
                              Type '(doc: S, ret: Record<string, any>, options: ToObjectOptions<S>) => any' is not assignable to type 'boolean | ((doc: PopulatedDoc<T, S>, ret: Record<string, any>, options: ToObjectOptions<PopulatedDoc<T, S>>) => any) | undefined'.
                                Type '(doc: S, ret: Record<string, any>, options: ToObjectOptions<S>) => any' is not assignable to type '(doc: PopulatedDoc<T, S>, ret: Record<string, any>, options: ToObjectOptions<PopulatedDoc<T, S>>) => any'.
                                  Types of parameters 'doc' and 'doc' are incompatible.
                                    Type 'PopulatedDoc<T, S>' is not assignable to type 'S'.
                                      'S' could be instantiated with an arbitrary type which could be unrelated to 'PopulatedDoc<T, S>'.

11 ): docs is mongoose.Types.Array<S> {
              ~~~~~~~~~~~~~~~~~~~~~~~

  src/test.ts:8:32
    8 export function isRefTypeArray<T, S extends mongoose.RefType>(
                                     ~
    This type parameter might need an `extends S` constraint.

(the typescript suggestion of This type parameter might need an "extends S" constraint. is not a solution)

Steps to Reproduce

/* eslint-disable @typescript-eslint/ban-types */
// NodeJS: 19.9.0
// MongoDB: 5.0 (Docker)
// Typescript 4.9.5
import * as mongoose from 'mongoose'; // mongoose@7.2.2

export function isRefTypeArray<T, S extends mongoose.RefType>(
  docs: mongoose.Types.Array<mongoose.PopulatedDoc<T, S>> | null | undefined
  // error on the next line
): docs is mongoose.Types.Array<S> {
  return Array.isArray(docs) && docs.every((v) => /* some testing func */ true);
}

Reproduction Repository & Branch: https://github.com/typegoose/typegoose-testing/tree/mongooseToObjectOptions

Expected Behavior

no typescript error

@hasezoey hasezoey added the typescript Types or Types-test related issue / Pull Request label Jun 5, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.2.3, 7.2.4 Jun 8, 2023
vkarpov15 added a commit that referenced this issue Jun 9, 2023
types: remove generic param that's causing issues for typegoose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants