首页 > 解决方案 > 忽略一个项目中的定义,但不忽略另一个

问题描述

我有一个带有node_modules文件夹的工作区和一个src包含我的客户端服务器打字稿代码的文件夹。它看起来像这样:

/node_modules
  /@types
    /node
    /requirejs
/src
  /client
    /admin
      /index.ts
      /tsconfig.json
    /public
      /index.ts
      /tsconfig.json
  /server
    /index.ts
    /tsconfig.json

我正在使用requirejs我的客户端代码和node我的服务器代码。他们每个人都有相似的类型,所以当我构建我的代码时,我得到以下错误:

node_modules/@types/node/index.d.ts(6860,5): error TS2300: Duplicate identifier 'mod'.
node_modules/@types/requirejs/index.d.ts(38,11): error TS2300: Duplicate identifier 'mod'.
node_modules/@types/requirejs/index.d.ts(422,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.

有没有办法让我的客户端代码忽略node定义,让我的服务器代码忽略requirejs定义?

标签: typescripttypescript-typings

解决方案


由于客户端和服务器构建具有单独tsconfig.json的文件,因此您只需设置types编译器选项以限制node_modules/@types在每个构建中自动加载的包。或者,您可以为两个构建设置单独node_modules的目录。如果您无法使其正常工作,请在您的问题中添加更多信息,我会再看一遍。


推荐阅读