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] TS error when declaring array path with { type: [{ type: String }] } #10731

Closed
thiagokisaki opened this issue Sep 15, 2021 · 1 comment
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@thiagokisaki
Copy link
Contributor

thiagokisaki commented Sep 15, 2021

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

What is the current behavior?
Currently, declaring an array path with { type: [{ type: String }] } causes a TS error.

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

import { Schema } from 'mongoose';

interface IProduct {
  keywords: string[];
}

const productSchema = new Schema<IProduct>({
  keywords: {
    type: [
      {
        type: String, // error in this line
        trim: true,
        lowercase: true,
        required: true,
      },
    ],
    required: true,
  },
});

TS error:

Type '{ type: { type: StringConstructor; trim: true; lowercase: true; required: true; }[]; required: true; }' is not assignable to type 'SchemaDefinitionProperty<string[]>'.
  Types of property 'type' are incompatible.
    Type '{ type: StringConstructor; trim: true; lowercase: true; required: true; }[]' is not assignable to type 'AnyArray<"string" | StringConstructor | "String" | typeof String>'.
      Type '{ type: StringConstructor; trim: true; lowercase: true; required: true; }[]' is not assignable to type '("string" | StringConstructor | "String" | typeof String)[]'.
        Type '{ type: StringConstructor; trim: true; lowercase: true; required: true; }' is not assignable to type '"string" | StringConstructor | "String" | typeof String'.
          Object literal may only specify known properties, and 'type' does not exist in type 'StringConstructor | typeof String'.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 TypeScript error.

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.6
TypeScript: v4.4.3
MongoDB: irrelevant

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Sep 15, 2021
@vkarpov15 vkarpov15 added this to the 6.0.8 milestone Sep 18, 2021
@vkarpov15
Copy link
Collaborator

Fixed, fix will be in 6.0.8 👍

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