Schema Type is inferred as a Partial when assigning schema definition to a variable
See original GitHub issuePrerequisites
- 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:
- Created a year ago
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mohammad0-0ahmad is correct, the right way to do this is
as const;
. TS is currently unable to figure out thatschemaDefinition
isn’t modified in any way, sonew mongoose.Schema(schemaDefintion);
is not equivalent tonew mongoose.Schema({ name: { type: String, required: true }, ... })
as far as TS is concerned, unless you add theas const;
.@heilmela test this: