-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Typescript: Group interface for type PipelineStage has conflicting index signatures #11059
Comments
I get the same problem with node version 16. Sticking to 6.0.14 for the moment as workaround. |
import mongoose, { Types } from 'mongoose';
const { Schema } = mongoose;
run().catch(console.error);
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
interface IUser {
_id: Types.ObjectId | string;
name?: string;
}
const userSchema = new Schema<IUser>({ name: String });
const User = mongoose.model<IUser>('User', userSchema);
await User.aggregate([
{
$group: { _id: '$name' }
}
])
} |
@ahmedelshenawy25 did not help |
If you want to reproduce, please try out my project at |
I worked around it by casting the pipeline to |
Casting to any did not work for me. |
@XopoIII I think @ahmedelshenawy25 just wanted to provide a reproduction script. This is just a minimalistic setup for getting the error. |
Upgrading from 6.0.14 to 6.1.1, all those TS errors appeared in aggregate pipelines: 1
2
3 (solved here)
4
5
|
@DavideViolante I would say cases 1, 2, 4 and 5 are coming from the reported issue with the Group interface. About number 3 I'm not sure, maybe open another ticket if you are sure, that this is a real bug. |
@DavideViolante In Case 3, you can ajust you code. You can use de code bellow with the same effect:
|
I have the same errors 1, 2 and 4 |
I think this PR #10971 caused these TS errors. Maybe @jeremyben can help to fix it. |
@DavideViolante Thanks for the heads up. First this is the interface Group {
$group: {
_id: any;
[key: string]: { [op in AccumulatorOperator]?: any }
}
} It seems, based on errors, that the compiler is unable to pin down the So we unfortunately have to lose the narrowing of operators when interface Group {
$group: { _id: any; [key: string]: any } | { [key: string]: { [op in AccumulatorOperator]?: any } }
} Or more radically we just go back to a mere index signature with interface Group {
$group: { [key: string]: any }
} |
Hi @jeremyben !
This will allow ommit the It's a trick problem. There is a discusion here #11104 For some reason (that I don't understand) this works (and the compiler reconizes the operator): interface Group {
$group: { _id: any} | {_id: any, [key: string]: { [op in AccumulatorOperator]?: any } }
} |
@rpenido Bravo, this is the one signature. But this is it ! interface Group {
$group: { _id: any } | { _id: any, [key: string]: { [op in AccumulatorOperator]?: any } }
} I honestly don't know either why this would work and not the first one. |
I tried the proposed solution in my code and it fixes the TS errors. I just sent a PR. ^ |
Shame. I thought I could call myself after my PR being merged contributor of mongoose. Well :D |
I have this issue upgrading from node Casting Specifically, pipeline stages like this no longer work, no matter what we do. Using Mongoose 6.4.1
|
@OllyDixon the below script compiles fine in Mongoose 6.4.1. Please open a new issue, follow the issue template, and modify the below script to demonstrate the issue you're seeing. import mongoose from 'mongoose';
run().catch(err => console.log(err));
async function run() {
const schema = new mongoose.Schema({ name: String });
const Test = mongoose.model('Test', schema);
await Test.aggregate([
{
$set: {
roomMembers: "$$ROOT",
},
},
]);
} |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
If you provide a "group" stage with the mandatory
_id
property in your aggregration pipeline, typescript will throw an error:If the current behavior is a bug, please provide the steps to reproduce.
tsconfig:
What is the expected behavior?
Property
_id
acceptsany
, instead of only accepting the object withAccumulatorOperator
as type.Typescript suggests to use a union of all property types, see: https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures
This is most probably connected to #10971
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
node version: 14
mongoose version: 6.1.0
mongo version: 4.2.12
typescript version: 4.5.2
The text was updated successfully, but these errors were encountered: