代码之家  ›  专栏  ›  技术社区  ›  Ben Grant John Hartsock

如何设置Typescript以从另一个Prisma文件夹导入类型?

  •  0
  • Ben Grant John Hartsock  · 技术社区  · 3 年前

    我们正在使用 Typescript , Prisma TRPC 在两个需要相互通信的NodeJS服务中。这两个服务都有自己的数据库,因此在中生成的Prisma客户端 node_modules 不同文件夹之间不同。文件夹结构设置如下:

    .
    ├── service_1/
    │   ├── tsconfig.json
    │   ├── package.json
    │   └── ...rest of project
    └── service_2/
        ├── tsconfig.json
        ├── package.json
        └── ...rest of project
    

    因为我们使用TRPC, service_1 直接从导入类型 service_2 如以下示例:

    import type { AppRouter } from '../../../../service_2/src/routers/index'
    

    这在VSCode中运行良好,但我们希望将类型检查作为CI操作 服务_1 ,但正在运行 tsc -p ./tsconfig.json --noEmit 在的根 服务_1 显示了许多类似的错误:

    ../service_2/src/services/dog.ts:53:10 - error TS2339: Property 'dog' does not exist on type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.
    
    53   prisma.dog.findMany({
    

    我们推测这是由Typescript使用生成的 @prisma/client 来自的程序包 服务_1 文件夹,即使在打字检查时 服务_2 ,但我们已经尝试更改了 tsconfig.json 但无济于事。

    我们的电流 tsconfig.json 在里面 服务_1 看起来是这样的:

    {
      "compilerOptions": {
        "jsx": "react-jsx",
        "strict": true,
        "paths": {
          "/*": ["./*"]
        },
        "baseUrl": "./",
        "noEmit": true,
        "esModuleInterop": true
      },
      "exclude": [
        "**/node_modules/*",
        "**/dist/*",
        "**/.git/*",
      ],
    }
    

    我们尝试过的一些选项包括:

    • 背景 rootDir ./
    • 背景 根目录 ../
    • 背景 rootDirs ["./", "../service_2"]
    • 正在添加 @ts-ignore @ts-nocheck 高于类型导入
    • 正在添加 服务_2 exclude 选项

    有没有一种方法可以通过单独的 节点模块 一个从另一个进口?

    0 回复  |  直到 3 年前