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

InferSchemaType: Allow null for optional fields #12748

Closed
2 tasks done
JavaScriptBach opened this issue Dec 1, 2022 · 2 comments · Fixed by #12781
Closed
2 tasks done

InferSchemaType: Allow null for optional fields #12748

JavaScriptBach opened this issue Dec 1, 2022 · 2 comments · Fixed by #12781
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@JavaScriptBach
Copy link
Contributor

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

Node.js version

16.17.1

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

Currently optional fields in schemas accept either the field type or undefined. Can we change this to also accept null? AFAIK null should be treated the same as undefined in this case, and it makes the types more usable. In our codebase, we generally treat null and undefined the same in our types, unless we're forced to do otherwise due to explicit behavior difference such as https://mongoosejs.com/docs/migrating_to_6.html#removed-omitundefined.

i.e. we are using

type Maybe<T> = T | null | undefined

Steps to Reproduce

If I define the following schema:

const ExampleSchema = new Schema({
  example: String
});
type ExampleSchemaFields = InferSchemaType<typeof ExampleSchema>;

It infers the following type:

// type ExampleSchemaFields = {
//   example?: string | undefined;
// }

Can we change this to example?: string | null | undefined?

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 6.7.7 milestone Dec 4, 2022
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Dec 4, 2022
@JavaScriptBach
Copy link
Contributor Author

@vkarpov15 I'm happy to help implement this change if you can give me a pointer of where to edit. I tried looking in https://github.com/Automattic/mongoose/blob/master/types/inferschematype.d.ts but couldn't find how we type optional fields.

@philiparvidsson
Copy link

Yes, this is causing bugs for us. null is entirely different from undefined (trivially, null can be understood as intentionally set to nothing)

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

Successfully merging a pull request may close this issue.

3 participants