Skip to content

$unionWith pipeline Typescript error #11069

@rpenido

Description

@rpenido

Using a pipeline in $unionWith throws a Typescript error.

Example:

const data = model.aggregate([
  {
    $match: {
      somevalue: "string"
    }
  },
  {
    $unionWith: {
      coll: "some.collection",
      pipeline: [
        {
          $match: {
            someField: "someValue"
          }
        }
      ]
    }
  }
]);

Error:

Type '{ $match: { someField: string; }; }[]' is not assignable to type 'AddFields | Bucket | BucketAuto | CollStats | Count | Facet | GeoNear | GraphLookup | Group | ... 20 more ... | undefined'.
  Property '$unwind' is missing in type '{ $match: { someField: string; }; }[]' but required in type 'Unwind'.ts(2322)

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "rootDir": "./src",
    "outDir": "./bin",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "exclude": [
    "**/__tests__/*.test.ts",
  ]
}

Code sandbox: https://codesandbox.io/s/admiring-tree-pjlze?file=/src/index.ts:96-361

The problem seems to be that the declaration of pipeline type is not an array.

mongoose/index.d.ts

Lines 3176 to 3181 in 61ad24d

export interface UnionWith {
/** [`$unionWith` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/unionWith/) */
$unionWith:
| string
| { coll: string; pipeline?: Exclude<PipelineStage, PipelineStage.Out | PipelineStage.Merge> }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions