多个具有相同名称的JSON文件的打字稿定义

时间:2018-07-03 22:15:06

标签: typescript typescript2.0

我正在尝试为存在于多个位置但内容不同的JSON文件创建定义。为了简单起见,我将此文件称为“ config.json”。

该项目使用yarn workspaces进行组织,因此基本结构如下:

package.json
config.json
config.d.ts
index.ts
...
src/
  server/
    package.json
    config.json
    config.d.ts
    index.ts
    ...
  client/
    package.json
    config.json
    config.d.ts
    index.ts
    ...

我的config.d.ts类似于:

declare module '*/config.json' {
    const config: RootConfig // in src/server, this is ServerConfig, etc.
    export = config
}

在此配置中,tsc和我的编辑器(Atom)抛出错误,表明模块config.json已被声明。具体来说,Duplicate identifier用于导出;重命名变量不会改变它。但是,ts-node/register和我的测试对此没有问题。

当我从config.d.tsserver目录中删除client文件时,tsc和Atom没有错误,但是它们使用了错误的定义(RootConfig随处可见)。另外,ts-node和我的测试失败,因为./config.json is not a module

如果我删除了模块名称上的星号,则会收到一个错误消息,即在环境模块中不能使用相对路径,或者找不到该模块。

为此结构设置定义的正确方法是什么?

0 个答案:

没有答案
相关问题