-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
BREAKING CHANGE: allow null for optional fields in TypeScript #13901
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i have some questions, mainly is adding | null
now required for ?:
properties which should already have a | undefined
added by ?
?
@@ -477,7 +477,7 @@ function gh12100() { | |||
const TestModel = model('test', schema_with_string_id); | |||
const obj = new TestModel(); | |||
|
|||
expectType<string>(obj._id); | |||
expectType<string | null>(obj._id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is null
here but not undefined
?
@hasezoey "is adding | null now required for ?: properties which should already have a | undefined added by ??" <-- yes. |
i know that they are separate and i know that the "more correct" way is |
Co-authored-by: hasezoey <[email protected]>
@hasezoey I don't understand "those that do not want to deal with null and only undefined (at least in terms of types) is it still required to be added? like is there some kind of type incompatibility to not add |null?". Can you please clarify with some examples? |
i mean is the following still possible (/ supported) without type errors? interface SomeSchema {
prop1?: string;
prop2?: number;
}
// instead of now
interface SomeSchema {
prop1?: string | null;
prop2?: number | null;
} which would trickle down to something like typegoose class SomeSchema {
@prop()
public prop1?: string;
@prop()
public prop2?: number;
}
// instead of
class SomeSchema {
@prop()
public prop1?: string | null;
@prop()
public prop2?: number | null;
} personally even if the more correct way would be TL;DR: |
@hasezoey you shouldn't need to change import mongoose from 'mongoose';
interface IUser {
prop?: string;
}
const schema = new mongoose.Schema<IUser>({ prop: String });
const TestModel = mongoose.model<IUser>('Test', schema);
const doc = new TestModel();
const foo: string | undefined | null = doc.prop; Can you please try this PR out with Typegoose and see if anything breaks? |
seems to run fine (both runtime and types), though i will need to test it a little more in depth (against the 8.0 branch in general) |
Summary
Apply #12748, #12781 to Mongoose 8. At runtime, Mongoose lets you store
null
for optional fields. TypeScript types should reflect this.Examples