You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
The Typescript type for SchemaTypeOptions.get() may be incorrect. Specifically this:
get?: (value: T,doc?: this)=>any;
If the current behavior is a bug, please provide the steps to reproduce.
Consider this schema definition for using Decimal128 represented as a BigNumber (via bignumber.js):
This code compiles just fine, but the val arg for get is typed with any, which isn't type-safe. I want it to be type-safe, so I inspected type of val, which is Decimal128 (the type stored in MongoDB). That makes total sense, so I updated the type accordingly:
error TS2322: Type '{ type: typeof mongoose.Schema.Types.Decimal128; get: (val: Decimal128) => BigNumber | undefined; set: (val: BigNumber) => mongoose.Types.Decimal128 | undefined; }' is not assignable to type 'SchemaDefinitionProperty<BigNumber>'.
Types of property 'get' are incompatible.
Type '(val: Decimal128) => BigNumber | undefined' is not assignable to type '((getter: (value: any) => any) => void) | (<K extends keyof SchemaOptions>(key: K) => SchemaOptions[K]) | ((value: BigNumber, doc?: SchemaTypeOptions<BigNumber> | undefined) => any) | undefined'.
Type '(val: Decimal128) => BigNumber | undefined' is not assignable to type '(getter: (value: any) => any) => void'.
Types of parameters 'val' and 'getter' are incompatible.
Type '(value: any) => any' is missing the following properties from type 'SchemaType': OptionsConstructor, cast, default, get, and 16 more.
The only way to get it to compile is to use unsafe any or to change the type to BigNumber, which isn't accurate. The object passed in is a Decimal128.
As I understand it, the get function should take the value stored in MongoDB (the value) and return the value per the type defined in the schema, so it seems the type of value and the return type are inverted.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 17.8.0
Mongoose: 6.2.8
MongoDB: 4.3.1
The text was updated successfully, but these errors were encountered:
@vkarpov15 thanks for fixing this issue! I wonder if my suggestion wasn't 100% correct, though. I wonder if the return type actually needs to be T | undefined so that undefined is a valid return type? As written now, you can only return an object of type T, which makes optional fields unsupported 🤦♂️
Sorry for not realizing this in my initial suggestion!
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
The Typescript type for
SchemaTypeOptions.get()
may be incorrect. Specifically this:If the current behavior is a bug, please provide the steps to reproduce.
Consider this schema definition for using
Decimal128
represented as aBigNumber
(via bignumber.js):This code compiles just fine, but the
val
arg forget
is typed withany
, which isn't type-safe. I want it to be type-safe, so I inspected type ofval
, which isDecimal128
(the type stored in MongoDB). That makes total sense, so I updated the type accordingly:With that change, it no longer compiles:
The only way to get it to compile is to use unsafe
any
or to change the type toBigNumber
, which isn't accurate. The object passed in is aDecimal128
.tsconfig.json
What is the expected behavior?
I believe the type should be this:
As I understand it, the
get
function should take the value stored in MongoDB (thevalue
) and return the value per the type defined in the schema, so it seems the type ofvalue
and the return type are inverted.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 17.8.0
Mongoose: 6.2.8
MongoDB: 4.3.1
The text was updated successfully, but these errors were encountered: