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] Error when declaring array of subdocuments with type: [{ type: childSchema }] #10789

Closed
thiagokisaki opened this issue Sep 27, 2021 · 0 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@thiagokisaki
Copy link
Contributor

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?
TypeScript error when declaring array of subdocuments with type: [{ type: childSchema }].

If the current behavior is a bug, please provide the steps to reproduce.

import { Schema } from 'mongoose';

interface IAddress {
  city: string;
  state: string;
  country: string;
}

interface IUser {
  name: string;
  addresses: IAddress[];
}

const addressSchema = new Schema<IAddress>({
  city: {
    type: String,
    required: true,
  },
  state: {
    type: String,
    required: true,
  },
  country: {
    type: String,
    required: true,
  },
});

const userSchema = new Schema<IUser>({
  name: {
    type: String,
    required: true,
  },
  addresses: {
    type: [
      {
        type: addressSchema, // error in this line
        required: true,
      },
    ],
    required: true,
  },
});
Type '{ type: { type: Schema<IAddress, Model<IAddress, any, any, any>, {}>; required: true; }[]; required: true; }' is not assignable to type 'SchemaDefinitionProperty<IAddress[]>'.
  Types of property 'type' are incompatible.
    Type '{ type: Schema<IAddress, Model<IAddress, any, any, any>, {}>; required: true; }[]' is not assignable to type 'AnyArray<Schema<any, Model<any, any, any, any>, {}>> | AnyArray<{ city?: SchemaDefinitionProperty<string>; state?: SchemaDefinitionProperty<string>; country?: SchemaDefinitionProperty<...>; }>'.
      Type '{ type: Schema<IAddress, Model<IAddress, any, any, any>, {}>; required: true; }[]' is not assignable to type 'Schema<any, Model<any, any, any, any>, {}>[]'.
        Type '{ type: Schema<IAddress, Model<IAddress, any, any, any>, {}>; required: true; }' is not assignable to type 'Schema<any, Model<any, any, any, any>, {}>'.
          Object literal may only specify known properties, and 'type' does not exist in type 'Schema<any, Model<any, any, any, any>, {}>'.ts(2322)

tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "rootDir": "src",
    "outDir": "dist",
    "baseUrl": "src",
    "paths": {
      "@/*": ["*"]
    },
    "module": "commonjs",
    "esModuleInterop": true,
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
}

What is the expected behavior?
No errors.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: v14.17.3
Mongoose: v6.0.8
TypeScript: v4.4.3
MongoDB: irrelevant

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Sep 27, 2021
@vkarpov15 vkarpov15 added this to the 6.0.10 milestone Sep 27, 2021
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

No branches or pull requests

3 participants