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.

Schema Type is inferred as a Partial when assigning schema definition to a variable

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.4.1

Node.js version

16.13.0

MongoDB server version

5.0

Description

When assigning the schema definition to a variable instead of directly passing it to the schema constructor the resulting type has all properties inferred as optionals.

Steps to Reproduce

import { Schema, InferSchemaType } from 'mongoose';

const schemaDefintion = {
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: String,
};

// Schema
const schema = new mongoose.Schema(schemaDefintion);

type User = InferSchemaType<typeof schema>;
// InferSchemaType will determine the type as follows:
//  type User = {
//     name?: string | undefined;
//     email?: string | undefined;
//     avatar?: string | undefined;
// }

Expected Behavior

type User = InferSchemaType<typeof schema>;
// InferSchemaType will determine the type as follows:
// type User = {
//     name: string;
//     email: string;
//     avatar?: string | undefined;
// }

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Jul 16, 2022

@mohammad0-0ahmad is correct, the right way to do this is as const;. TS is currently unable to figure out that schemaDefinition isn’t modified in any way, so new mongoose.Schema(schemaDefintion); is not equivalent to new mongoose.Schema({ name: { type: String, required: true }, ... }) as far as TS is concerned, unless you add the as const;.

1reaction
mohammad0-0ahmadcommented, Jul 4, 2022

@heilmela test this:

import { Schema, InferSchemaType } from 'mongoose';

const schemaDefintion = {
  name: { type: String, required: true },
  email: { type: String, required: true },
  avatar: String,
} as const;

const schema = new mongoose.Schema(schemaDefintion);

type User = InferSchemaType<typeof schema>;
Read more comments on GitHub >

github_iconTop Results From Across the Web

W3C XML Schema Definition Language (XSD) 1.1 Part 2
This specification defines datatypes that can be used in an XML Schema. These datatypes can be specified for element content that would be ......
Read more >
Schema Variables - Actian Documentation
The type of schema to create. By default, a variable length schema is created (useful for delimited text). To create a fixed-width text...
Read more >
Spark Schema - Explained with Examples
Spark schema is the structure of the DataFrame or Dataset, we can define it using StructType class which is a collection of StructField...
Read more >
Spark read parquet with custom schema - Stack Overflow
If you're going to specify a custom schema you must make sure that schema matches the data you are reading. In your example...
Read more >
The Z Notation: - UMD Department of Computer Science |
2.3.2 Schemas with global variables. 36. 2.4. Generic constructions. 38. 2.5. Partially-defined expressions. 40. 3. The Z Language.
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