如何将本地 NPM 包 TS 类型用于环境模块 (Handlebars)?

How to use local NPM package TS typings for ambient module (Handlebars)?

我正在尝试将 npm i handlebars 集成到我现有的 TS (v 3.4.5) 项目中,但始终得到 TS2307: Cannot find module 'handlebars'..

我在分布在各种文件中的几个不同模块中使用 Handlebars。

根据这个 merged feature the maintainers imported typings from DefinitelyTyped into the handlebars repo and they depreciated the @types/handlebars 包。

TS 似乎无法找到 repo 类型并从上方显示 TS2307 错误。

我能够获得干净编译的唯一方法是手动将 .d.ts 文件从存储库复制到自制的 @types/handlebars 文件夹中。

到时候我只要做其中之一:

在仅 1 个使用 Handlebars 的文件中,我能够生成无错误构建。

我的 tsconfig.json 看起来像这样:

{
  "compileOnSave": false,
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": true,
    "module": "es6",
    "target": "es5"
  },
  "include": [
    "./Scripts/app/**/*",
  ],
  "exclude": [
    "node_modules",
  ]
}

如果不手动复制 .d.ts 文件,似乎没有任何效果。

所以我有两个问题:

  1. 让 Typescript 识别这个不需要手册的环境模块的正确方法是什么 copy/paste?

  2. 为什么TS只需要其中一个使用Handlebars的模块中的import语句?难道不是所有引用它的模块都需要它吗?

谢谢。

基于此 link 的 TS 文档:

Now we can /// node.d.ts and then load the modules using import url = require("url"); or import * as URL from "url".

这似乎是引用 @types 目录中不存在的环境模块的唯一方法。导入语句不是编译它所必需的。

作为记录,我还尝试更新 tsconfig.json 的包含路径以引用 .d.ts 但它被忽略了。