我正在开发一个使用 Mongoose 并使用
tsc
进行编译的 TypeScript 项目。当我尝试使用带有 .find()
的查询助手时:
import { InferRawDocType, Schema, model } from "mongoose";
export const mySchemaDefinition = {
key: "string",
value: "string"
} as const;
export type RawSchema = InferRawDocType<typeof mySchemaDefinition>;
export const mySchema = new Schema(mySchemaDefinition, {
query: {
byKey: function (key: string) {
return this.where({ key: key });
}
}
});
export const MyModel = model("MyModel", mySchema);
MyModel.find({ key: "123" }).exec(); // OK
MyModel.findOne().byKey("123").exec(); // OK
MyModel.find().byKey("123").exec(); // Error
TypeScript 在转译过程中抛出错误:
The 'this' context of type 'QueryWithHelpers<(Document<unknown, { byKey: <T extends Query<unknown, Document<unknown, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>> & FlatRecord<{ readonly key: "string"; readonly value: "string"; }> & { _id: ObjectId; } & { __v: number; }, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>, "find", Record<string, never>>>(this: T, key: string) => T; }, { readonly key: "string"; readonly value: "string"; }> & { readonly key: "string"; readonly value: "string";
} & { _id: ObjectId; } & { __v: number; })[], Document<unknown, { byKey: <T extends Query<unknown, Document<unknown, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>> & FlatRecord<{ readonly key: "string"; readonly value: "string"; }> & { _id: ObjectId; } & { __v: number; }, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>, "find", Record<string, never>>>(this: T, key: string) => T; }, { readonly key: "string"; readonly value: "string"; }> & { readonly key: "string"; readonly value: "string"; } & { _id: ObjectId; } & { __v: number; }, { byKey: <T extends Query<unknown, Document<unknown, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>> & FlatRecord<{ readonly key: "string"; readonly value: "string"; }> & { _id: ObjectId; } & { __v: number; }, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>, "find", Record<string, never>>>(this: T, key: string) => T; }, { readonly key: "string"; readonly value: "string"; }, "find", {}>' is not assignable to method's 'this' of type 'Query<unknown, Document<unknown, {}, FlatRecord<{ readonly key: "string"; readonly value: "string";
}>> & FlatRecord<{ readonly key: "string"; readonly value: "string"; }> & { _id: ObjectId; } & { __v: number; }, {}, FlatRecord<{ readonly key: "string"; readonly value: "string"; }>, "find", Record<string, never>>'.
The types returned by 'lean(...).exec()' are incompatible between these types.
Type 'Promise<({ readonly key: "string"; readonly value: "string"; } & { _id: ObjectId; } & { __v: number; })[]>' is not assignable to type 'Promise<{ readonly key: "string"; readonly value: "string"; } & { _id: ObjectId; } & { __v: number; }>'.
Type '({ readonly key: "string"; readonly value: "string"; } & { _id: ObjectId; } & { __v: number; })[]' is not assignable to type '{ readonly key: "string"; readonly value: "string"; } &
{ _id: ObjectId; } & { __v: number; }'.
Type '({ readonly key: "string"; readonly value: "string"; } & { _id: ObjectId; } & { __v: number; })[]' is missing the following properties from type '{ readonly key: "string"; readonly value: "string"; }': key, value
但它只发生在
.find
,.findOne
工作正常。this
上下文似乎是正确的类型。tsc
配置的问题还是有我可以修复的 Mongoose 类型错误?
构建命令:
tsc index.ts --esModuleInterop --outDir ./build --noErrorTruncation
tsconfig.json
(主要针对同一项目中的 React 应用程序):
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"]
}
发生错误的原因是 byKey 查询助手的类型不正确,无法同时处理 find() 和 findOne() 操作。当前的实现假定单个文档返回类型,但 find() 返回一个数组。让我们修复打字:
export const mySchemaDefinition = {
key: "string",
value: "string"
} as const;
export const mySchema = new Schema(mySchemaDefinition, {
query: {
byKey: function<T extends Query<any, any>>(this: T, key: string) {
return this.where({ key: key });
}
}
});
此更改使得 byKey 查询助手足够通用,可以与 find() 和 findOne() 操作一起使用。