Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.95k stars 3.84k forks source link

Typescript - error when using getters #10863

Closed ashisherc closed 3 years ago

ashisherc commented 3 years ago

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

What is the current behavior?

Type '{ type: typeof mongoose.Types.Decimal128; get: (l: mongoose.Types.Decimal128) => string; }' is not assignable to type 'SchemaDefinitionProperty<Decimal128> | undefined'.
  Types of property 'type' are incompatible.
    Type 'typeof Decimal128' is not assignable to type 'typeof SchemaType | Schema<any, any, any> | Decimal128 | { _bsontype?: SchemaDefinitionProperty<"Decimal128"> | undefined; readonly bytes?: SchemaDefinitionProperty<...> | undefined; toString?: SchemaDefinitionProperty<...> | undefined; toJSON?: SchemaDefinitionProperty<...> | undefined; inspect?: SchemaDefinitionPr...'.
      Type 'typeof Decimal128' is missing the following properties from type 'typeof SchemaType': cast, checkRequired, set, getts(2322)

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

import * as mongoose from 'mongoose';

interface User {
  name: string
  amount: mongoose.Types.Decimal128;
}

const UserSchema = new mongoose.Schema<User>(
  {
    name: { type: String },
    amount: {
      type: mongoose.Types.Decimal128,
      get: (l: mongoose.Types.Decimal128) => l && l.toString(),
    },
  }
);
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
  },
  "include": ["src"]
}

What is the expected behavior? should not throw any error

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version. Node - 14.17.0, Mongoose 6.0.10, Mongodb - 4.2, TS - 4.4.2

ashisherc commented 3 years ago

using mongoose.Schema.Types.Decimal128 solves the error