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

Typescript errors in 6.0.0 #10605

Closed
SammyWhamy opened this issue Aug 25, 2021 · 5 comments
Closed

Typescript errors in 6.0.0 #10605

SammyWhamy opened this issue Aug 25, 2021 · 5 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@SammyWhamy
Copy link

Do you want to request a feature or report a bug?
A bug

What is the current behavior?
Multiple typescript types seem to not be working well, or at least they are not documented in the migration guide.
For now, I used //@ts-ignore to suppress the issues, and I haven't run into any errors, leading me to believe this is just a bug in the typings.

If the current behavior is a bug, please provide the steps to reproduce.
Find on array value:

Model.findOne({arrayField: "arrayValue"});
// TS2322: Type 'string' is not assignable to type 'Condition  | undefined'.

Model with nested field:

interface ITest extends mongoose.Document {
    object: {
        value: number
    }
}

const test = new mongoose.Schema<ITest>({
    object: {
        type: {
            value: {
                type: Number,
            }
        }
    }
});
/*
TS2322: Type '{ type: { value: { type: NumberConstructor; }; }; }' is not assignable to type
'typeof SchemaType | Function[] | Schema<any, any, any> | Schema<any, any, any>[] | readonly Schema<any, any, any>[] | SchemaTypeOptions<{ value: number; }> | ... 5 more ... | undefined'.
Types of property 'type' are incompatible. 
Type '{ value: { type: NumberConstructor; }; }' is not assignable to type '{ value: number; } | typeof SchemaType | Schema<any, any, any> | undefined'.
Types of property 'value' are incompatible.
Type '{ type: NumberConstructor; }' is not assignable to type 'number'.
*/

Static methods this:

schema.static('method', function(): {
    this.findOne();
    // TS2571: Object is of type 'unknown'.
});

My tsconfig:

{
  "extends": "@tsconfig/node16/tsconfig.json",
  "compilerOptions": {
    "target": "ESNext",
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "src",
    "moduleResolution": "Node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "ESNext",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "baseUrl": "src",
  },
  "exclude": [
    "./tests"
  ]
}

What is the expected behavior?
These types don't show errors, or the changes are documented with an alternate way to do these things instead.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 16.6.1
Typescript: 4.3.5
MongoDB: 5.0.2
Mongoose: 6.0.0

@vkarpov15 vkarpov15 modified the milestones: 5.13.9, 6.0.1 Aug 25, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 25, 2021
@vkarpov15
Copy link
Collaborator

Confirmed all 3 issues with the below script. We're investigating and will have a fix or workaround later today 👍

import { connect, model, Schema } from 'mongoose';
  
run().catch(err => console.log(err));

async function run() {
  await connect('mongodb://localhost:27017/test');

  interface ITest {
    arrayField?: string[];
    object: {
        value: number
    };
  }
  const schema = new Schema<ITest>({
    arrayField: [String],
    object: {
        type: {
            value: {
                type: Number,
            }
        }
    }
  });
  schema.static('method', function() {
    this.findOne();
  });

  const TestModel = model<ITest>('Test', schema);

  await TestModel.findOne({ arrayField: 'some value' });
}

@vkarpov15
Copy link
Collaborator

Can confirm the above script compiles with d862a5a.

As a workaround until we ship 6.0.1, you can do:

For 1:

await TestModel.findOne({ arrayField: { $all: ['some value'] } });

For 2:

  const schema = new Schema<ITest>({
    arrayField: [String],
    object: new Schema({
            value: {
                type: Number,
            }
        })
  });

(3) goes away when you fix (2).

@SammyWhamy
Copy link
Author

Hey, I'm still having very similar issues as 2, except now only in places where i use an array of objects as a type, such as:

roles: {
    type: [{
        id: {
            type: String
        }
    }]
}

Again, //@ts-ignore'ing these issues doesn't cause any errors in runtime.
In schemas with this issue, the this bug I mentioned occurs as well

@SammyWhamy
Copy link
Author

Small snippet to reproduce:

interface ITestSchema extends mongoose.Document {
    someObject: Array<{id: string}>
}

const testSchema = new mongoose.Schema<ITestSchema>({
    someObject: {
        type: [{
            id: {
                type: String,
            },
        }], 
    },
});
/*
TS2322: Type '{ type: { id: { type: StringConstructor; }; }[]; }' is not assignable to type 'typeof SchemaType | Function[] | Schema<any, any, any> | Schema<any, any, any>[] | readonly Schema<any, any, any>[] | SchemaTypeOptions<{ id: string; }[]> | ... 5 more ... | undefined'.
Types of property 'type' are incompatible.
Type '{ id: { type: StringConstructor; }; }[]' is not assignable to type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>[] | readonly Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>[] | Schema<Document<any, any, any> & { ...; }, Model<...>, {}>[] | readonly Schema<...>[] | undefined'.
Type '{ id: { type: StringConstructor; }; }[]' is not assignable to type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>[]'.
Type '{ id: { type: StringConstructor; }; }' is not assignable to type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>'.
Object literal may only specify known properties, and 'id' does not exist in type 'Schema<{ id: string; }, Model<{ id: string; }, any, any>, {}>'.
*/

@aleneum
Copy link

aleneum commented Sep 3, 2021

I am also facing typing issues after upgrading from mongoose 5 to 6.

Mongoose: 6.0.4
Typescript: 4.4.2

import mongoose, { Schema, Document } from "mongoose";

interface ITestSchema extends Document {
  anArray: string[];
}

async function main() {
  const TestSchema: Schema = new Schema();
  TestSchema.add({
    anArray: [
      {
        type: String,
      },
    ],
  });

  const TestModel = mongoose.model<ITestSchema>("Test", TestSchema);
  await TestModel.findOne({ anArray: { $in: ["valueA", "valueB"] } }).exec();
  // Type '{ $in: string[]; }' is not assignable to type 'Condition<string[]> | undefined'.
  // Types of property '$in' are incompatible.
  // Type 'string[]' is not assignable to type 'string[][]'.ts(2322)
}

@vkarpov15 vkarpov15 reopened this Sep 4, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.4, 6.0.5 Sep 4, 2021
@Automattic Automattic locked as resolved and limited conversation to collaborators Sep 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants