Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/typescript/schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ There are a few caveats for using automatic type inference:

1. You need to set `strictNullChecks: true` or `strict: true` in your `tsconfig.json`. Or, if you're setting flags at the command line, `--strictNullChecks` or `--strict`. There are [known issues](https://github.com/Automattic/mongoose/issues/12420) with automatic type inference with strict mode disabled.
2. You need to define your schema in the `new Schema()` call. Don't assign your schema definition to a temporary variable. Doing something like `const schemaDefinition = { name: String }; const schema = new Schema(schemaDefinition);` will not work.
3. Mongoose adds `createdAt` and `updatedAt` to your schema if you specify the `timestamps` option in your schema, *except* if you also specify `methods`, `virtuals`, or `statics`. There is a [known issue](https://github.com/Automattic/mongoose/issues/12807) with type inference with timestamps and methods/virtuals/statics options. If you use methods, virtuals, and statics, you're responsible for adding `createdAt` and `updatedAt` to your schema definition.
3. Mongoose adds `createdAt` and `updatedAt` to your schema if you specify the `timestamps` option in your schema.

If you must define your schema separately, use [as const](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions) (`const schemaDefinition = { ... } as const;`) to prevent *type widening*. TypeScript will automatically widen types like `required: false` to `required: boolean`, which will cause Mongoose to assume the field is required. Using `as const` forces TypeScript to retain these types.

Expand Down
12 changes: 7 additions & 5 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,13 @@ function testInferTimestamps() {
});

type WithTimestamps2 = InferSchemaType<typeof schema2>;
// For some reason, expectType<{ createdAt: Date, updatedAt: Date, name?: string }> throws
// an error "Parameter type { createdAt: Date; updatedAt: Date; name?: string | undefined; }
// is not identical to argument type { createdAt: NativeDate; updatedAt: NativeDate; } &
// { name?: string | undefined; }"
expectType<{ name?: string | null }>({} as WithTimestamps2);
expectType<{ createdAt: Date; updatedAt: Date } & { name?: string | null }>({} as WithTimestamps2);

const TestModel = model('Test', schema2);
const doc = new TestModel({ name: 'test' });
expectType<string | undefined | null>(doc.name);

expectType<string | undefined | null>(doc.myName());
}

function gh12431() {
Expand Down
5 changes: 1 addition & 4 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ declare module 'mongoose' {

type ApplySchemaOptions<T, O = DefaultSchemaOptions> = ResolveTimestamps<T, O>;

type ResolveTimestamps<T, O> = O extends { methods: any } | { statics: any } | { virtuals: any } | { timestamps?: false } ? T
// For some reason, TypeScript sets all the document properties to unknown
// if we use methods, statics, or virtuals. So avoid inferring timestamps
// if any of these are set for now. See gh-12807
type ResolveTimestamps<T, O> = O extends { timestamps?: false } ? T
: O extends { timestamps: infer TimestampOptions } ? TimestampOptions extends true
? { createdAt: NativeDate; updatedAt: NativeDate; } & T
: TimestampOptions extends SchemaTimestampsConfig
Expand Down