Typescript 日常填坑

601 阅读1分钟

记录在日常使用 ts 遇到的问题 & 解决方式

导入 json 文件

场景:项目中把配置存放到 json 文件,在模块中导入使用

问题

  • 使用 ESM 的方式导入模块报错 Consider using '--resolveJsonModule' to import module with '.json' extension.
  • 根据错误提示,设置resolveJsonModule 配置,出现新的错误提示 This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag. image.png

解决:

  • 在 tsconfig.json 配置的 compilerOptions 字段中添加 resolveJsonModuleesModuleInterop ,值设置为 true

image.png