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/mongoose] MongooseUpdateQuery.push? is undefined #49563

Closed
3 tasks done
notaphplover opened this issue Nov 14, 2020 · 1 comment
Closed
3 tasks done

[@types/mongoose] MongooseUpdateQuery.push? is undefined #49563

notaphplover opened this issue Nov 14, 2020 · 1 comment

Comments

@notaphplover
Copy link
Contributor

notaphplover commented Nov 14, 2020

Given any type T extends Document, Pick<MongooseUpdateQuery<Document>, '$push'> is equal to { $push?: undefined; }.

I expect to be able to use MongooseUpdateQuery to push elements in a subdocument.

Minimal example:

import { Document, MongooseUpdateQuery } from 'mongoose';

interface Foo extends Document {
  nonArrayProperty: number;
  bar: number[];
}

const query: MongooseUpdateQuery<Foo> = {
  $push: {
    bar: 2,
  },
};

Playground Link

On the other hand, no errors are thrown applying the same process to a type T which does not extends the Document class:

import { MongooseUpdateQuery } from 'mongoose';

interface Foo {
  nonArrayProperty: number;
  bar: number[];
}

const query: MongooseUpdateQuery<Foo> = {
  $push: {
    bar: 2,
  },
};

Playground Link

Is this the expected behavior?

Additional information

It seems there is a conflict at mongodb PushOperator type when TSchema extends Document. PushOperator<Document> is equal to never.

If we explore the mongodb PushOperator type:

export type PushOperator<TSchema> = ({
    readonly [key in KeysOfAType<TSchema, ReadonlyArray<any>>]?:
        | UpdateOptionalId<Unpacked<TSchema[key]>>
        | ArrayOperator<Array<UpdateOptionalId<Unpacked<TSchema[key]>>>>;
} &
    NotAcceptedFields<TSchema, ReadonlyArray<any>>) & {
    readonly [key: string]: ArrayOperator<any> | any;
};

We can see two intersections of types. If we try:

type SimplifiedPushOperator<TSchema> = ({
    readonly [key in KeysOfAType<TSchema, ReadonlyArray<any>>]?:
        | UpdateOptionalId<Unpacked<TSchema[key]>>
        | ArrayOperator<Array<UpdateOptionalId<Unpacked<TSchema[key]>>>>;
} &
    NotAcceptedFields<TSchema, ReadonlyArray<any>>);

type DocumentSimplifiedPushOperator = SimplifiedPushOperator<Document>

The type DocumentSimplifiedPushOperator is equal to never, so we can asume there must be a conflict in that type intersection.

Additional information 2

Looking at your types:

 export type MongooseUpdateQuery<S> = mongodb.UpdateQuery<S> & mongodb.MatchKeysAndValues<S>;

The following change would probably solve the issue:

- export type MongooseUpdateQuery<S> = mongodb.UpdateQuery<S> & mongodb.MatchKeysAndValues<S>;
+ export type MongooseUpdateQuery<S> = mongodb.UpdateQuery<DocumentDefinition<S>> & mongodb.MatchKeysAndValues<DocumentDefinition<S>>;

But there's probably a good reason to not to do that change.

Additional information 3

It seems you have UpdateQuery which is

export type UpdateQuery<D> = MongooseUpdateQuery<DocumentDefinition<D>>;

It feels a bit confusing, it's not easy to figure out when I should use MongooseUpdateQuery over UpdateQuery.

@orta orta closed this as completed Jun 7, 2021
@orta
Copy link
Collaborator

orta commented Jun 7, 2021

Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

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

2 participants