Sub-schema Type Mismatch
See original GitHub issueDo you want to request a feature or report a bug? Bug
What is the current behavior? Schema inside schema (sub-schema) type mismatch
If the current behavior is a bug, please provide the steps to reproduce.
There is a Schema referring to GeoLocation, as per mongoose documentation, this following PointSchema.ts
file is created
// PointSchema.ts ======
import { Document, Schema } from "mongoose";
export interface IGeoPoint {
type: string
coordinates: Array<number>
}
export interface IPoint extends IGeoPoint, Document {
getGeoCoordinates(): IGeoCoordinate
}
const pointSchema: Schema<IPoint> = new Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
});
export interface IGeoCoordinate {
lat: number
lon: number
}
pointSchema.methods.getGeoCoordinates = function (this:IPoint): IGeoCoordinate {
return {
lat: this.coordinates[0],
lon: this.coordinates[1]
}
}
export default pointSchema
and another file LocationModel.ts
which uses this PointsSchema.ts
is created
// LocationModel.ts ======
import { Document, model, Schema } from "mongoose";
import PointSchema, { IPoint, IGeoCoordinate, IGeoPoint } from "../Schemas/PointSchema";
export interface ILocationDocument extends Document {
location: IPoint
name: string
address: string
getGeoCoordinate(): IGeoCoordinate
}
const LocationSchema: Schema<ILocationDocument> = new Schema({
location: PointSchema, // <---- error at this line
name: String,
address: String
})
LocationSchema.methods.getGeoCoordinate = function (this: ILocationDocument): IGeoCoordinate {
return this.location.getGeoCoordinates()
}
export default model<ILocationDocument>("location", LocationSchema, 'locations')
Error at the marked line is:
Argument of type '{ location: Schema<IPoint, Model<IPoint>>; name: StringConstructor; address: StringConstructor; }' is not assignable to parameter of type '{ [path: string]: SchemaDefinitionProperty<undefined>; }'.
Property 'location' is incompatible with index signature.
Type 'Schema<IPoint, Model<IPoint>>' is not assignable to type 'SchemaDefinitionProperty<undefined>'.
Type 'Schema<IPoint, Model<IPoint>>' is not assignable to type 'Schema<Document<any>, Model<Document<any>>>'.
Types of property 'methods' are incompatible.
Type '{ getGeoCoordinates: () => IGeoCoordinate; type: string; coordinates: number[]; _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; ... 48 more ...; validateSync: (pathsToValidate?: string[] | undefined, opti...' is not assignable to type '{ _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; $isEmpty: (path: string) => boolean; ... 47 more ...; validateSync: (pathsToValidate?: string[] | undefined, options?: any) => CallbackError; } & { ...; }'.
Type '{ getGeoCoordinates: () => IGeoCoordinate; type: string; coordinates: number[]; _id?: any; __v?: number | undefined; $ignore: (path: string) => void; $isDefault: (path: string) => boolean; $isDeleted: (val?: boolean | undefined) => boolean; ... 48 more ...; validateSync: (pathsToValidate?: string[] | undefined, opti...' is not assignable to type '{ [name: string]: (this: Document<any>, ...args: any[]) => any; }'.
Index signatures are incompatible.
Type '(this: IPoint, ...args: any[]) => any' is not assignable to type '(this: Document<any>, ...args: any[]) => any'.
The 'this' types of each signature are incompatible.
Type 'Document<any>' is not assignable to type 'IPoint'.
My tsconfig.json
file:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
}
}
What is the expected behavior? Sub-schema should be included by adapting the type of the schema
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node: v14.7.0 mongoose: 5.11.11 @types/mongoose: 5.10.3 MongoDB: Atlas (version 4.2.11)
This issue was not seen in mongoose v5.9.13, and is seen in v5.11.11 (on probable enforcement of #9717)
Please update the documentation as per the updates and please let me know what I am missing here
Issue Analytics
- State:
- Created 3 years ago
- Comments:8
I took a closer look at this and managed to confirm the issue, looks like I needed to run just
tsc
, nottsc ./LocationModel.ts
to get this error. It looks like we already fixed this issue in #9862, so this issue will be fixed in v5.11.15, which we will ship in the next couple of days. Thanks for your patience!Thanks for the action, looking forward to the update