gpt4 book ai didi

typescript - 在服务器和客户端之间共享一个公共(public)包

转载 作者:搜寻专家 更新时间:2023-10-30 21:02:04 24 4
gpt4 key购买 nike

我想要以下项目设置:三个顶级文件夹 client、common 和 server。它们都是一个单独的包,有自己的 package.json 和 node_modules 文件夹。

对于下面的示例,我使用了这种结构:

- common
- node_modules
- package.json
- entities.ts

- server
- node_modules
- package.json
- app.ts
- tsconfig.json

然后我尝试在服务器文件夹中运行 tsc

客户端和服务器都应使用的文件将位于公共(public)文件夹中,我想使用 ES6 模块语法从服务器和客户端导入它们,如 import {MyModel} from 'common/entities' ;

问题是我不知道如何设置 typescript 来理解这个设置。

我已使用 npm link 使 common 在服务器中显示为普通的 node_module,但上述导入生成错误 Cannot find module 'common/entities'。我尝试将 "moduleResolution": "node" 添加到 compilerOptions 但没有结果。

是否有可能让 typescript 理解此设置,或者是否有适合此设置的设置?

最佳答案

Is there possible to get typescript to understand this setup or is there a setup that is preffered to this?

我倾向于只使用一个单个 tsconfig.json 和一个node_modules。然后注意不要在前端使用 commonclient 之外的东西。

它仍然比没有 TypeScript 的安全性要好。

关于typescript - 在服务器和客户端之间共享一个公共(public)包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com