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

SchemaDefinitionProperty doesn't work with number type #9863

Closed
devagrawal09 opened this issue Jan 25, 2021 · 2 comments
Closed

SchemaDefinitionProperty doesn't work with number type #9863

devagrawal09 opened this issue Jan 25, 2021 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@devagrawal09
Copy link

Do you want to request a feature or report a bug?
Bug (typescript)
What is the current behavior?
Sample code:

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

interface IProfile { age: number; }

interface ProfileDoc extends Document, IProfile {}

const ProfileSchemaDef: SchemaDefinition<IProfile> = { age: Number };

export const ProfileSchema = new Schema<ProfileDoc>(ProfileSchemaDef);

The last line gives me these errors:

Argument of type '{ age: SchemaDefinitionProperty<number>; }' is not assignable to parameter of type '{ [path: string]: SchemaDefinitionProperty<undefined>; }'.
  Property 'age' is incompatible with index signature.
    Type 'SchemaDefinitionProperty<number>' is not assignable to type 'SchemaDefinitionProperty<undefined>'.
      Type 'number' is not assignable to type 'SchemaDefinitionProperty<undefined>'.

Error goes away if I change the interface definition to

interface IProfile { age: Number; }

But tslint's recommended rules include banning the usage of the Number type.

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

Code provided above

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib":["ES2020"],
    "allowJs": true,
    "outDir": "build",
    "rootDir": ".",
    "sourceMap": true,

    "moduleResolution": "node",
    "baseUrl": "./src",
    "esModuleInterop": true,

    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    
    "preserveConstEnums": true,
    "resolveJsonModule": true,
    "pretty": true,
  },
  "include": ["src", "test"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

What is the expected behavior?
The interface can be defined with number type and typescript does not throw errors.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node v12.15.0, mongoose v5.11.11

@vkarpov15 vkarpov15 added this to the 5.11.15 milestone Jan 28, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jan 28, 2021
@vkarpov15
Copy link
Collaborator

One potential workaround is to just get rid of the generic param to SchemaDefinition: const ProfileSchemaDef: SchemaDefinition = { age: Number }; . We took a closer look at this and it seems like the best way to get this to work correctly is by adding a 3rd generic param to Schema that helps with typechecking your schema definition as suggested in #9789

@vkarpov15
Copy link
Collaborator

You'll have to do this to help Mongoose infer the type of age re: #9789:

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

interface IProfile { age: number; }

interface ProfileDoc extends Document, IProfile {}

const ProfileSchemaDef: SchemaDefinition<IProfile> = { age: Number };

export const ProfileSchema = new Schema<ProfileDoc, Model<ProfileDoc>, ProfileDoc>(ProfileSchemaDef);

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

2 participants