【发布时间】:2021-04-13 01:28:30
【问题描述】:
我有以下代码:
// NoteList.server.js
import {prisma} from './db.server'; // how to type this using JSDoc???
import SidebarNote from './SidebarNote';
export default function NoteList({searchText}) {
const notes = prisma.note.findMany({
where: {
title: {
contains: searchText ?? undefined,
},
},
});
// Now let's see how the Suspense boundary above lets us not block on this.
// fetch('http://localhost:4000/sleep/3000');
return notes.length > 0 ? (
<ul className="notes-list">
{notes.map((note) => (
<li key={note.id}>
<SidebarNote note={note} />
</li>
))}
</ul>
) : (
<div className="notes-empty">
{searchText
? `Couldn't find any notes titled "${searchText}".`
: 'No notes created yet!'}{' '}
</div>
);
}
导入的prisma 对象是我的node_modules 中PrismaClient 类的一个实例。我也有一个index.d.ts,所以我希望输入prisma 对象,以便在VS Code 中自动完成。
我知道我可以使用 JSDoc 导入PrismaClient 类型如下:
/**
* @typedef { import("@prisma/client").PrismaClient } PrismaClient
*/
但是,我现在很难在导入时将 PrismaClient 类型分配给 prisma 对象。我知道在将类型传递给函数时如何将类型分配给对象/值:
/**
* @param {PrismaClient} prisma - My `PrismaClient` instance
*/
function foo(prisma) {
// I now get autocompletion on the `prisma` instance
// because VS Code knows it's of type `PrismaClient`
}
但我不知道如何为导入做同样的事情。谁能帮我吗?这是JSDoc cheatsheet 的链接,以防万一。
编辑:另外,为了添加更多信息,prisma 实例是从该文件导入的:
// db.server.js
import {PrismaClient} from 'react-prisma';
export const prisma = new PrismaClient();
您还可以在此处找到包含此示例代码的 repo:https://github.com/prisma/server-components-demo
感谢@Mino 的建议,我也在导出时尝试了这个,但我仍然没有得到任何自动补全:
/**
* @typedef { import("@prisma/client").PrismaClient } PrismaClient
*/
import {PrismaClient} from 'react-prisma';
/**
* @const {PrismaClient}
*/
export const prisma = new PrismaClient();
【问题讨论】:
标签: javascript typescript visual-studio-code typescript-typings jsdoc