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

@types/mongoose create schema forces non-required properties at crate time #45608

Closed
3 tasks done
tomyam1 opened this issue Jun 20, 2020 · 5 comments
Closed
3 tasks done

Comments

@tomyam1
Copy link
Contributor

tomyam1 commented Jun 20, 2020

In the example below, addedInMiddleware is required on the document but not on the create schema since it's added in a middleware.

The schema added in 88a54a9 causes addedInMiddleware to be required at compile time, but it shouldn't be.

@andreialecu I'd appreciate if you can fix this.

import * as Mongoose from 'mongoose'

interface IDocument extends Mongoose.Document {
    addedInMiddleware: string
}

const schema = new Mongoose.Schema({
    addedInMiddleware: String,
})

schema.pre('validate', async function (this: IDocument) {
    if (!this.addedInMiddleware) this.addedInMiddleware = 'added';
});

const model = Mongoose.model<IDocument>('model', schema);

/*
TS2769: No overload matches this call.   
The last overload gave the following error.     
Argument of type '{}' is not assignable to parameter of type 'Pick<{ _id: any; addedInMiddleware: string; } & { _id: any; }, "addedInMiddleware"> & { _id?: any; }'.       
Property 'addedInMiddleware' is missing in type '{}' but required in type 'Pick<{ _id: any; addedInMiddleware: string; } & { _id: any; }, "addedInMiddleware">'.
 */
model.create({});
@andreialecu
Copy link
Contributor

Proper usage here is:

type CreateDocument = Omit<IDocument, "addedInMiddleware">;
model.create<CreateDocument>({});

Or define your interface like:

interface IDocument extends Mongoose.Document {
    addedInMiddleware?: string
}

@tomyam1
Copy link
Contributor Author

tomyam1 commented Jun 20, 2020

Sorry, neither of your suggestions are proper usage.

Proper usage here is:

type CreateDocument = Omit<IDocument, "addedInMiddleware">;
model.create<CreateDocument>({});

This will NOT work for nested objects.

Or define your interface like:

interface IDocument extends Mongoose.Document {
    addedInMiddleware?: string
}

This is just wrong. addedInMiddleware is not optional in the doucment.

tomyam1 added a commit to tomyam1/DefinitelyTyped that referenced this issue Jun 20, 2020
This reverts the (second) attempt made at adding schema for document creation because it
causes valid well-typed code to break.

The previous attempt was made at:
DefinitelyTyped@26587aa#diff-f77e4c92e873042528b4ef79285cdf3c

And reverted at:
DefinitelyTyped@c860c1d#diff-f77e4c92e873042528b4ef79285cdf3c

This attempt suffers from the same faults as the previous attempt and causes valid,
well-typed, code to break which should never happen in a types library.

Some of them are listed here in the previous PR I made to remove this type checking
DefinitelyTyped#37823

fixes DefinitelyTyped#45305
fixes DefinitelyTyped#45609
fixes DefinitelyTyped#45608

revert 88a54a9
@andreialecu
Copy link
Contributor

andreialecu commented Jun 20, 2020

interface IDocument extends Mongoose.Document {
    readonly addedInMiddleware: string
}

This will also work if it's read only, meaning addedInMiddleware will not be required during creation.

tomyam1 added a commit to tomyam1/DefinitelyTyped that referenced this issue Jun 20, 2020
This reverts the (second) attempt made at adding schema for document creation because it
causes valid well-typed code to break.

The previous attempt was made at:
DefinitelyTyped@26587aa#diff-f77e4c92e873042528b4ef79285cdf3c

And reverted at:
DefinitelyTyped@c860c1d#diff-f77e4c92e873042528b4ef79285cdf3c

This attempt suffers from the same faults as the previous attempt and causes valid,
well-typed, code to break which should never happen in a types library.

Some of them are listed here in the previous PR I made to remove this type checking
DefinitelyTyped#37823

fixes DefinitelyTyped#45305
fixes DefinitelyTyped#45609
fixes DefinitelyTyped#45608

revert 88a54a9
@andreialecu
Copy link
Contributor

This will NOT work for nested objects.

Of course it will, you can override the type at any level in the schema.

Here's a helper type to help with that:
piotrwitek/utility-types#129

tomyam1 added a commit to tomyam1/DefinitelyTyped that referenced this issue Jun 20, 2020
This reverts the (second) attempt made at adding schema for document creation because it
causes valid well-typed code to break.

The previous attempt was made at:
DefinitelyTyped@26587aa#diff-f77e4c92e873042528b4ef79285cdf3c

And reverted at:
DefinitelyTyped@c860c1d#diff-f77e4c92e873042528b4ef79285cdf3c

This attempt suffers from the same faults as the previous attempt and causes valid,
well-typed, code to break which should never happen in a types library.

Some of them are listed here in the previous PR I made to remove this type checking
DefinitelyTyped#37823

fixes DefinitelyTyped#45305
fixes DefinitelyTyped#45609
fixes DefinitelyTyped#45608

revert 88a54a9
tomyam1 added a commit to tomyam1/DefinitelyTyped that referenced this issue Jun 20, 2020
This reverts the (second) attempt made at adding schema for document creation because it
causes valid well-typed code to break.

The previous attempt was made at:
DefinitelyTyped@26587aa#diff-f77e4c92e873042528b4ef79285cdf3c

And reverted at:
DefinitelyTyped@c860c1d#diff-f77e4c92e873042528b4ef79285cdf3c

This attempt suffers from the same faults as the previous attempt and causes valid,
well-typed, code to break which should never happen in a types library.

Some of them are listed here in the previous PR I made to remove this type checking
DefinitelyTyped#37823

fixes DefinitelyTyped#45305
fixes DefinitelyTyped#45609
fixes DefinitelyTyped#45608

revert 88a54a9
tomyam1 added a commit to tomyam1/DefinitelyTyped that referenced this issue Jun 20, 2020
This reverts the (second) attempt made at adding schema for document creation because it
causes valid well-typed code to break.

The previous attempt was made at:
DefinitelyTyped@26587aa#diff-f77e4c92e873042528b4ef79285cdf3c

And reverted at:
DefinitelyTyped@c860c1d#diff-f77e4c92e873042528b4ef79285cdf3c

This attempt suffers from the same faults as the previous attempt and causes valid,
well-typed, code to break which should never happen in a types library.

Some of them are listed here in the previous PR I made to remove this type checking
DefinitelyTyped#37823

fixes DefinitelyTyped#45305
fixes DefinitelyTyped#45609
fixes DefinitelyTyped#45608

revert 88a54a9
@orta
Copy link
Collaborator

orta commented Jun 7, 2021

Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

@orta orta closed this as completed Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants