Skip to content

Instantly share code, notes, and snippets.

@andreialecu
Last active May 8, 2020 11:45
Show Gist options
  • Save andreialecu/9740c56dde6cbae2e0afc3cd0dc3d1e7 to your computer and use it in GitHub Desktop.
Save andreialecu/9740c56dde6cbae2e0afc3cd0dc3d1e7 to your computer and use it in GitHub Desktop.
@types/mongoose typed model.create()
diff --git a/node_modules/@types/mongoose/index.d.ts b/node_modules/@types/mongoose/index.d.ts
index c430d7b..639365e 100644
--- a/node_modules/@types/mongoose/index.d.ts
+++ b/node_modules/@types/mongoose/index.d.ts
@@ -83,8 +83,14 @@ declare module "mongoose" {
// We can use TypeScript Omit once minimum required TypeScript Version is above 3.5
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
+ type NonFunctionPropertyNames<T> = {
+ [K in keyof T]: T[K] extends Function ? never : K;
+ }[keyof T];
+
+ type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
+
/* Helper type to extract a definition type from a Document type */
- type DocumentDefinition<T> = Omit<T, Exclude<keyof Document, '_id'>>;
+ type DocumentDefinition<T> = NonFunctionProperties<Omit<T, Exclude<keyof Document, '_id'>>>;
/**
* Patched version of FilterQuery to also allow:
@@ -122,6 +128,13 @@ declare module "mongoose" {
export type UpdateQuery<D> = MongooseUpdateQuery<DocumentDefinition<D>>;
+ export type InsertQuery<D> = mongodb.OptionalId<DocumentDefinition<D>>;
+
+ type Exactly<T, U> = T & Record<Exclude<keyof U, keyof T>, never>;
+
+ export type InsertPartialOverride<T, TOptionalId = mongodb.OptionalId<T>> = Exactly<Partial<TOptionalId>, TOptionalId>;
+
+
/**
* Gets and optionally overwrites the function used to pluralize collection names
* @param fn function to use for pluralization of collection names
@@ -2212,7 +2225,7 @@ declare module "mongoose" {
*
* @param err optional error to throw if no docs match `filter`
*/
- orFail(err?: Error | (() => Error)): this;
+ orFail(err?: Error | (() => Error)): DocumentQuery<NonNullable<T>, DocType, QueryHelpers>;
/** Specifies a $polygon condition */
polygon(...coordinatePairs: number[][]): this;
@@ -3020,7 +3033,7 @@ declare module "mongoose" {
* Model#ensureIndexes. If an error occurred it is passed with the event.
* The fields, options, and index name are also passed.
*/
- new(doc?: any): T;
+ new(doc?: InsertQuery<T>): T;
/**
* Requires a replica set running MongoDB >= 3.6.0. Watches the underlying collection for changes using MongoDB change streams.
@@ -3119,10 +3132,10 @@ declare module "mongoose" {
* does new MyModel(doc).save() for every doc in docs.
* Triggers the save() hook.
*/
- create(docs: any[], callback?: (err: any, res: T[]) => void): Promise<T[]>;
- create(docs: any[], options?: SaveOptions, callback?: (err: any, res: T[]) => void): Promise<T[]>;
- create(...docs: any[]): Promise<T>;
- create(...docsWithCallback: any[]): Promise<T>;
+ create<TDoc extends InsertPartialOverride<T> = mongodb.OptionalId<T>>(docs: InsertQuery<TDoc>[], callback?: (err: any, res: T[]) => void): Promise<T[]>;
+ create<TDoc extends InsertPartialOverride<T> = mongodb.OptionalId<T>>(docs: InsertQuery<TDoc>[], options?: SaveOptions, callback?: (err: any, res: T[]) => void): Promise<T[]>;
+ create<TDoc extends InsertPartialOverride<T> = mongodb.OptionalId<T>>(...docs: InsertQuery<TDoc>[]): Promise<T>;
+ create<TDoc extends InsertPartialOverride<T> = mongodb.OptionalId<T>>(...docsWithCallback: InsertQuery<TDoc>[]): Promise<T>;
/**
* Create the collection for this model. By default, if no indexes are specified, mongoose will not create the
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment