From c7f72739436acc09b46220aacd72274f0bb10f96 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 10 Sep 2024 11:31:21 -0400 Subject: [PATCH 1/2] types: make SchemaType static `setters` property accessible in TypeScript Fix #14879 --- test/types/schema.test.ts | 4 ++++ types/schematypes.d.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 04828bc4f17..5a399a0c7db 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -1634,3 +1634,7 @@ function gh14825() { type SchemaType = InferSchemaType; expectAssignable({} as SchemaType); } + +function gh14879() { + Schema.Types.String.setters.push((val?: unknown) => typeof val === 'string' ? val.trim() : val); +} diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index e8a0ecffdf0..37e255443fc 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -216,6 +216,9 @@ declare module 'mongoose' { /** Attaches a getter for all instances of this schema type. */ static get(getter: (value: any) => any): void; + /** Array containing default setters for all instances of this SchemaType */ + static setters: Function[]; + /** The class that Mongoose uses internally to instantiate this SchemaType's `options` property. */ OptionsConstructor: SchemaTypeOptions; From 60541e7480ac4ce2b3d69cc7a5f41e1d3322385e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 11 Sep 2024 11:45:38 -0400 Subject: [PATCH 2/2] types: add function args to setters property --- types/schematypes.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/schematypes.d.ts b/types/schematypes.d.ts index 37e255443fc..f5ca8ec0da9 100644 --- a/types/schematypes.d.ts +++ b/types/schematypes.d.ts @@ -217,7 +217,7 @@ declare module 'mongoose' { static get(getter: (value: any) => any): void; /** Array containing default setters for all instances of this SchemaType */ - static setters: Function[]; + static setters: ((val?: unknown, priorVal?: unknown, doc?: Document, options?: Record | null) => unknown)[]; /** The class that Mongoose uses internally to instantiate this SchemaType's `options` property. */ OptionsConstructor: SchemaTypeOptions;