question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`mongoose.InferSchemaType` does not correctly infer nullable arrays

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.5

Node.js version

16.15.1

MongoDB server version

5.0.12

Description

When working with Mongoose schemas involving array fields that are nullable, mongoose.InferSchemaType does not seem to correctly infer the schema type. The notion of optionality does not seem to be reflected.

Steps to Reproduce

Let’s suppose we create a Mongoose schema with 2 fields of 2 different types, one being an array of strings, the other an array of objects, both optional.

const TestSchema = new mongoose.Schema(
  {
    comments: { type: [String], default: () => undefined, required: false },
    reference: {
      type: [
        {
          type: { type: String, required: false },
          value: { type: String, required: false },
        },
      ],
      default: () => undefined,
      required: false,
    },
  },
  {
    collection: 'tests',
    timestamps: false,
  },
);

export type TestType = mongoose.InferSchemaType<typeof TestSchema>;

In TestType, the notion of optionality of our fields seems to be lost: inferred_type

The problem is the same if we replace default: () => undefined by default: () => null.

If we just try default: undefined or default: null, another problem appears: inferred_type_2

Perhaps there is a misunderstanding/error on my side in defining the schema. If this is the case, I would be happy to receive your advices. Otherwise, it must be a bug? 🐛

Expected Behavior

{
    comments?: string[];
    reference?: {
        type?: string;
        value?: string;
    }[];
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Uzlopakcommented, Sep 12, 2022
1reaction
vkarpov15commented, Dec 22, 2022

Confirmed that this issue still happens if strict: true isn’t set. Following script fails to compile without --strict. We’re looking into that to see if we can fix it.

import mongoose from 'mongoose';

const TestSchema = new mongoose.Schema(
  {
    comments: { type: [String], default: () => undefined, required: false },
    reference: {
      type: [
        {
          type: { type: String, required: false },
          value: { type: String, required: false },
        },
      ],
      default: () => undefined,
      required: false,
    },
  },
  {
    collection: 'tests',
    timestamps: false,
  },
);

export type TestType = mongoose.InferSchemaType<typeof TestSchema>;
const doc: TestType = { comments: undefined };
Read more comments on GitHub >

github_iconTop Results From Across the Web

tsc doesn't recognize virtuals on mongoose scheme
TSC doesn't recognize them as a field in the interface. I tried in both suggested manners (see code below). import {connect, InferSchemaType, ...
Read more >
Quick Start Guide - typegoose
This Guide is for Typegoose version ~10.0. Typegoose is a "wrapper" for easily writing Mongoose models with TypeScript. Instead of writing this:.
Read more >
mongoose
fix(document): handle setting array to itself after saving and pushing a new ... docs(subdocs): clarify that populated docs are not subdocs #12521 #12398 ......
Read more >
Mongoose v6.8.2: SchemaTypes
ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a ... The correct way to define an object with a property type...
Read more >
Strongly typed models with Mongoose and TypeScript
How to create strongly typed Mongoose models with TypeScript. There are two main ways to make your models strongly typed, Typegoose & and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found