dcs*_*san 1 orm typescript prisma
我试图在运行时在 prisma 中定义一个查询,但陷入了第一个障碍。
所以这有效:
const items = await prisma.styleTags.findMany({
orderBy: {
name: 'asc',
}
});
Run Code Online (Sandbox Code Playgroud)
但是当我尝试单独定义查询时,我收到 TS 错误。
const orderBy = {
cname: 'asc',
}
const items2 = await prisma.styleTags.findMany({
orderBy
});
Run Code Online (Sandbox Code Playgroud)
这两件事应该是相同的吗?但在自动生成代码的 prisma 迷宫深处的某个地方......
Type '{ cname: string; priority: string; }' is not assignable to type 'Enumerable<StyleTagsOrderByWithRelationInput> | undefined'.
Type '{ cname: string; priority: string; }' is not assignable to type 'StyleTagsOrderByWithRelationInput'.
Types of property 'cname' are incompatible.
Type 'string' is not assignable to type 'SortOrder | undefined'.
Run Code Online (Sandbox Code Playgroud)
我以为Type 'string' is not assignable to type 'SortOrder | undefined'.也许我可以通过orderBy: 'name',但那也失败了。
FWIW,如果我的@ts-ignore代码有效,但如果必须忽略 prisma 类型检查,那么它就没有什么用处。
之后我的下一步是尝试动态组合orderBy传入的参数,但我需要首先了解上述基础知识才能工作。
有人可以建议为什么 prisma 的类型检查失败吗?
您可以使用这些解决方法 -
orderBy,as const以阻止typescript将顺序推断为字符串。Prisma.orderBy.asc而不是“asc”。