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

type compatibility error of schema defination #10030

Closed
liudaoping opened this issue Mar 16, 2021 · 0 comments
Closed

type compatibility error of schema defination #10030

liudaoping opened this issue Mar 16, 2021 · 0 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@liudaoping
Copy link

node version: v14.15.1
typescript version: v4.2.3
mongoose version: v5.12.0

image

codes

interface IUser {
  id: string;
  name: string;
}
const userDef: SchemaDefinition<IUser> = {
  id: { type: String, required: true },
  name: { type: String, required: true },
};

const userSchema = new Schema(userDef, { _id: false, versionKey: false });

interface ICode {
  value: string;
  category: ObjectId;
  approved: boolean;
  createdBy: IUser;
  createdAt: Date;
}

const codeDef: SchemaDefinition<ICode> = {
  value: { type: SchemaTypes.String, required: true },
  category: { type: SchemaTypes.ObjectId, required: true },
  approved: { type: SchemaTypes.Boolean, required: true },
  createdBy: { type: userSchema, required: true },
  createdAt: { type: SchemaTypes.Date, required: true },
};

const codeSchema = new Schema(codeDef);

error details

  • field category
Type '{ type: typeof Schema.Types.ObjectId; required: true; }' is not assignable to type 'typeof SchemaType | Function[] | SchemaTypeOptions<ObjectId> | Schema<Document<any, {}>, Model<any, any>, undefined> | Schema<...>[] | SchemaTypeOptions<...>[] | { ...; } | { ...; }[]'.
  Types of property 'type' are incompatible.
    Type 'typeof ObjectId' is missing the following properties from type 'ObjectId': auto, default, immutable, index, and 8 more.ts(2322)
  • field approved
Type '{ type: typeof Schema.Types.Boolean; required: true; }' is not assignable to type 'boolean | typeof SchemaType | Function[] | SchemaTypeOptions<false> | Schema<Document<any, {}>, Model<any, any>, undefined> | ... 7 more ... | true[]'.
  Types of property 'type' are incompatible.
    Type 'typeof Boolean' is not assignable to type 'boolean'.ts(2322)
  • field createdBy
Type '{ type: Schema<Document<any, {}>, Model<any, any>, undefined>; required: true; }' is not assignable to type '{ id?: "string" | StringConstructor | "String" | typeof String | SchemaTypeOptions<string>; name?: "string" | StringConstructor | "String" | typeof String | SchemaTypeOptions<...>; } | ... 6 more ... | { ...; }[]'.
  Types of property 'type' are incompatible.
    Type 'Schema<Document<any, {}>, Model<any, any>, undefined>' is missing the following properties from type 'IUser': id, namets(2322)
  • field createdAt
Type '{ type: typeof Schema.Types.Date; required: true; }' is not assignable to type 'typeof SchemaType | Function[] | SchemaTypeOptions<Date> | Schema<Document<any, {}>, Model<any, any>, undefined> | Schema<...>[] | SchemaTypeOptions<...>[] | { ...; } | { ...; }[]'.
  Types of property 'type' are incompatible.
    Type 'typeof Date' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 38 more.ts(2322)
@IslandRhythms IslandRhythms added typescript Types or Types-test related issue / Pull Request confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 16, 2021
@vkarpov15 vkarpov15 added this to the 5.12.2 milestone Mar 19, 2021
@vkarpov15 vkarpov15 removed the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Mar 19, 2021
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