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.

Typescript generic schema usage

See original GitHub issue

Do you want to request a feature or report a bug? BUG

What is the current behavior? Typescript isn’t shouting on missing properties, but it does shouting on extra properties or different naming properties.

If the current behavior is a bug, please provide the steps to reproduce. image

What is the expected behavior? Typescript isn’t shouting on missing properties, but it does shouting on extra properties or different naming properties. For example, in the above code, property b is missing in the schema, everything seems to work. (typescript compiled without errros)

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version.

  • Node LTS (14.17)
  • Mongo 4.3
  • Mongoose 6.0.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Sep 20, 2021

@GalDayan you can work around this by using a separate type definition for the schema definition object as shown below.

import { Schema, SchemaDefinition, Model, model } from 'mongoose';
  
interface IContract {
  a: string;
  b: string;
}

// Below will fail to compile if `a` or `b` are omitted
// error TS2741: Property 'b' is missing in type '{ a: StringConstructor; }' but required in type 'Required<{ a?: SchemaDefinitionProperty<string>; b?: SchemaDefinitionProperty<string>; }>'
const schemaDef: Required<SchemaDefinition<IContract>> = { a: String, b: String };

const schema = new Schema<IContract>(schemaDef);
0reactions
GalDayancommented, Sep 7, 2021

Got ya. Is there any option to make it required and not partial? @vkarpov15

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Use Generics in TypeScript - DigitalOcean
When creating interfaces and classes in TypeScript, it can be useful to use generic type parameters to set the shape of the resulting...
Read more >
Typescript Generics Explained - Ross Bulat - Medium
The implementation of generics in Typescript give us the ability to pass in a range of types to a component, adding an extra...
Read more >
Documentation - Generics - TypeScript
When you begin to use generics, you'll notice that when you create generic functions like identity , the compiler will enforce that you...
Read more >
Define TypeScript type based on generic type - Stack Overflow
Depending on ContentType I am saving different JSON schema into data column into my database. Example: For IMAGE , saving {"path": "/foo/bar", " ......
Read more >
In-Depth Look at TypeScript Generics: Part 1 — Intro to Generics
In this two-part series, we will take an in-depth look at TypeScript generics. This first post will give you a basic understanding of ......
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