如何定义或让 Typescript 忽略相关的环境模块?

How to define or have Typescript ignore relative ambient modules?

我正在尝试使用自定义 json 加载程序模块加载 json 文件。我将 babel 配置为专门针对 src/routes.json 使用我的加载程序,但实际上它可以用于任何 json 文件。我已尽最大努力查看文档和报告的多个问题,这些问题与加载供应商库模块的类似问题有关,但我找到的 none 解决方案(定义模块的各种方式)正在为我.

我想做的是实现这里的声明式路由器:https://github.com/kriasoft/react-static-boilerplate/blob/master/docs/routing-and-navigation.md

我的问题是如何让 typescript 理解我的亲戚 src/routes.json

你可以这样写

declare module "*.json";

在全局 .ts.d.ts 文件中。这告诉 TypeScript "anything that ends in .json exists, and I shouldn't get any errors for using it."

澄清一下,当我说 "a global file" 时,我指的是没有导入或导出的任何文件。