francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
102 stars 24 forks source link

`default: null` ignored #142

Closed adrianwiechec-at-optilyz closed 3 months ago

adrianwiechec-at-optilyz commented 4 months ago

Another default problem, sorry :(

For:

import { Schema, model } from "mongoose";
const UserSchema = new Schema({
        nullableField: { type: String, required: true, default: null },
});
export const User = model("User", UserSchema);

mtgen gives us:

export type User = {
  nullableField: string;
  _id: mongoose.Types.ObjectId;
};

completely ignoring the possibility of null...

francescov1 commented 4 months ago

Hey @adrianwiechec-at-optilyz, thanks for the submission!

So this behavior is technically correct with simple typescript configs, as a "null" value is allowed to be set for a type like string. But if the user has strictNullChecks enabled in their tsconfig, then you are right that this would be a bug.

Either way, I agree with you that this type output is not clear. It would be better if the resulting type was "string | null". Let me try to add it in this weekend.

francescov1 commented 3 months ago

Fixed in v9.3.1 🚀

adrianwiechec-at-optilyz commented 3 months ago

Thank you, once again! :bow: