I have an npm dependency (electron-is-accelerator
) in my Typescript project that comes with no types, neither as part of the package, nor as part of DefinitelyTyped. The dependency consists of a module with just one function accepting a string and returning a boolean. I wrote a type definition in src/@types/electron-is-accelerator
as described in this article:
declare module 'electron-is-accelerator' {
export function isAccelerator(s: string): boolean;
}
I tried to execute this function like this:
import { isAccelerator } from 'electron-is-accelerator';
console.log(isAccelerator("Alt+Space"));
But I get the following runtime error:
TypeError: electron_is_accelerator_1.isAccelerator is not a function
How can I get this to run?
My tsconfig.json
:
{
"compilerOptions": {
"outDir": "./built",
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"sourceMap": false
},
"include": [
"src/**/*"
]
}
I uploaded a minimal demonstration example here for anyone who wants to play with the problem.
That package uses CommonJS module.exports
syntax to export the function:
module.exports = function (str) { ... };
module.exports
is different from ES modules default export
syntax and your named export declaration in the example above, so TS has export =
, import = require()
to express these imports/exports. electron-is-accelerator
can be typed with following declaration:
declare module "electron-is-accelerator" {
function isAccelerator(s: string): boolean;
export = isAccelerator;
}
The import looks like this:
import isAccelerator = require("electron-is-accelerator")
isAccelerator("whatever")
If esModuleInterop
is enabled, you can write it as ES default import:
import isAccelerator from "electron-is-accelerator"