我正在尝试使用 electron-windows-notifications
模块将 native Windows 通知添加到 Zulip Electron。
所以我正在阅读 https://github.com/felixrieseberg/electron-windows-notifications/blob/master/samples/shortcut.js
给出的示例代码,它创建了一个快捷方式应用程序,并且是通知正常工作所必需的。
代码如下:
const shortcut = require('node-win-shortcut')
const appId = 'electron-windows-notifications'
shortcut.createShortcut(process.execPath, 'node', appId)
我运行了 npm install node-win-shortcut
来安装该软件包。但是,当我将 const Shortcut = require('node-win-shortcut');
添加到我的文件时,VS Code 显示错误:
找不到模块“node-win-shortcut”的声明文件。 '.../zulip-electron/node_modules/node-win-shortcut/index.js' 隐式具有“any”类型。
尝试“npm install @types/node-win-shortcut”(如果存在)或添加包含“declare module 'node-win-shortcut'”的新声明(.d.ts)文件;
这确实是正确的,因为 node-win-shortcut/index.js
只是:
module.exports = require('./build/Release/node_win_shortcut_bindings.node');
我不知道如何访问 createShortcut()
方法。我怀疑我的 node-win-shortcut
安装尚未完全完成,并且一些构建过程仍然存在。
最佳答案
您似乎正在使用 TypeScript。当您想要使用 TypeScript 中的 JS 库(使用像 noImplicitAny
这样的严格检查)时,您必须拥有要使用的模块的类型声明。 DefinitelyTyped项目GitHub为 NPM 包提供类型声明文件,这些文件本身并不存在,并且它们在 @types organization 下发布在 NPM 上。如果包未附带类型定义,您可以查找 here查明是否有包裹。
但是,您需要的包似乎不包括在内。您可以做的是编写自己的类型定义,以便可以在 TypeScript 中使用这个库。仅涵盖您需要的方法的最小类型声明如下所示:
declare module 'node-win-shortcut' {
function createShortcut(path: String, name: String, appId: String): void
}
如果您将此类型声明放入文件中(即 node-win-shorcut.d.ts
)并将其导入您需要的位置,那么您应该可以顺利进行。
如果由于任何原因这不起作用,您可以使用DefinitelyTyped类型声明作为引用来建模您自己的类型,例如this very simple one对于 is-number
包。
作为一个单独的注释,您在 JavaScript 代码中看不到该方法本身的原因是绑定(bind)实际上是 built on installation来自 native 代码you can see here (正如您可以想象的,在 Windows 上创建快捷方式需要必须在 Windows 系统上运行的 native 代码绑定(bind) - npm i node-win-shorcut
在 Linux 或 MacOS 上会失败)。您可以查看 node_modules/node-win-shortcut
目录,以查看从 index.js
文件引用的文件中构建的工件。
关于node.js - 如何使用 npm 安装 node-win-shortcut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54923137/