2

I am trying to add native Windows notifications to Zulip Electron using the electron-windows-notifications module.

So I was reading the sample code given at https://github.com/felixrieseberg/electron-windows-notifications/blob/master/samples/shortcut.js which creates a shortcut for the app, and is a required for notifications to work.

The code is given below as well:

const shortcut = require('node-win-shortcut')
const appId = 'electron-windows-notifications'

shortcut.createShortcut(process.execPath, 'node', appId)

I ran npm install node-win-shortcut to install the package. However, when I add const shortcut = require('node-win-shortcut'); to my file, VS Code shows the error:

Could not find a declaration file for module 'node-win-shortcut'. '.../zulip-electron/node_modules/node-win-shortcut/index.js' implicitly has an 'any' type. Try 'npm install @types/node-win-shortcut' if it exists or add a new declaration (.d.ts) file containing 'declare module 'node-win-shortcut'';

This is indeed correct as node-win-shortcut/index.js just has:

module.exports = require('./build/Release/node_win_shortcut_bindings.node');

I don't know how to access the createShortcut() method. I suspect that my install of node-win-shortcut is not fully complete, and some build process still remains.

1 Answers1

3

It looks like you are using TypeScript. When you want to use JS libraries from TypeScript (using strict checks like noImplicitAny), you have to have the type declarations for the modules you want to use. The DefinitelyTyped project on GitHub provides type declaration files for NPM packages that don't on their own and they are published under the @types organization on NPM. If a package doesn't ship with type definitions, you can look it up here to find out whether there is a package with them.

The package you need, however, seem to not be covered. What you can do is write your own type definitions so that you can use this library in TypeScript. A minimal type declaration that would cover just the method you need would look like the following:

declare module 'node-win-shortcut' {
  function createShortcut(path: String, name: String, appId: String): void
}

If you put this type declaration in a file (i.e. node-win-shorcut.d.ts) and import it where you need you should be good to go.

If for any reason this is not working, you can use DefinitelyTyped type declarations as a reference for you to model your own, like this very simple one for the is-number package.


As a separate note, the reason why you don't see the method itself in the JavaScript code is that the bindings are actually built on installation from the native code you can see here (as you can imagine, creating a shortcut on Windows require native code bindings that has to be run on a Windows system -- npm i node-win-shorcut would fail on Linux or MacOS). You can have a look in the node_modules/node-win-shortcut directory to see the built artifact in the file referenced from the index.js file.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63