-
Notifications
You must be signed in to change notification settings - Fork 23
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
Does optional fields should be marked null? #5
Comments
Does it cause any issues? User.create({
title: 'a',
author: 1,
});
new User({
title: 'a',
author: 1,
}).save(); in both cases, arguments are From create(...docs: any[]): Promise<T>; Not sure if we should override it. Currently, static typing is only useful when we type |
I see another use case for static typing: type ExtractInterfaceFromDoc<T> = Pick<T, Exclude<keyof T, keyof Document>>;
const exampleSchema = createSchema({
firstName: Type.string(),
lastName: Type.string(),
middleName: Type.optionalString()
});
export const exampleModel = typedModel('example', exampleSchema);
export type IExampleDoc = ExtractDoc<typeof exampleModel>;
export type IExample = ExtractInterfaceFromDoc<IExampleDoc>;
const exampleInstance: IExample = {
firstName: 'first',
lastName: 'last'
} Creating |
Ok, I see. This is a valid situation. Added in 0.0.13 See here full example https://github.com/BetterCallSky/ts-mongoose#extracting-document-type |
Woah! Amazing :) There is one edge case left, if there are nested objects with optional properties, like this: const exampleSchema = createSchema({
firstName: Type.string(),
lastName: Type.string(),
profile: Type.object().of({
picture: Type.string(),
phone: Type.optionalString()
})
}); ExtractProps will still receive {
firstName: string;
lastName: string;
profile: {
picture: string;
phone: string | null | undefined;
};
} & {} instead of this (according to current way of working): {
firstName: string;
lastName: string;
} & {
profile: {
picture: string;
phone?: string | null | undefined;
}
} |
Can you check 0.0.14? There are more null/undefined fixes. |
Now it is working fine, no error return on creating following typed variable: const example: IExampleProps = {
firstName: 'a',
lastName: 'b',
profile: {
picture: 'a'
}
} but I'm not sure about type information provided on hover in vscode, because in previous version optional fields where marked with question mark, and now they are not. It doesn't matter if this property is nested or not. |
VSCode sometimes doesn't show final type in the popup. |
One more question, according to topic title: any reason for putting null to parameter types? Shouldn't be just type for required parameters and type | undefined for optional ones? |
Does it cause any problems during development? if the type is optional, and you want to remove the value, you can do: user.phone = null;
user.save(); Setting to undefined sometimes is not working as expected in some cases (but I am not sure). user.phone = undefined; |
Is it better to use typical object approach in this case? delete user.phone;
user.save(); |
Optional fields are not marked as |
I was wondering what was behind the idea of representing optional fields as type | null? Pure optional fields in types/interfaces are marked with question mark and return undefined if they're not exist.
The text was updated successfully, but these errors were encountered: