javascriptnode.jstypescriptinputprompt

Convert prompt-sync require into import method


I use prompt-sync module in my Node project.

 const prompt = require('prompt-sync')();
 const result = prompt(message);

But to keep my TypeScript code consistent I need to use import instead of require. So I installed types for the package.

npm i @types/prompt-sync

And I tried to use it like

import * as promptSync from 'prompt-sync';
...
const prompt = promptSync();
const result = prompt(message);

But the error appeared

Error:(24, 20) TS2349: This expression is not callable.
Type '{ default: (config?: Config | undefined) => Prompt; }' has no call signatures.

So how can I use prompt-sync with import?


Solution

  • The error is raised because you cannot call a namespace import (* as ns). This restriction is per the ECMAScript specification which mandates that module namespace objects, such as the aforementioned syntax creates, cannot have a [[Call]] or [[Construct]] signature.

    This results in a mismatch when attempting to consume CommonJS modules from ES modules as many of the former export a single function or constructor as the module itself (i.e. module.exports = function () {}).

    However, there is interop capability specified and conventionalized which works by synthesizing a default export for the CommonJS module that contains the value of module.exports.

    You can and should leverage this interop facility.

    Firstly, ensure that "esModuleInterop" is specified with a value of true in your tsconfig.json under "compilerOptions".

    Secondly, rewrite your code to import the synthetic default from the prompt-sync module

    import promptSync from 'prompt-sync';
    
    const prompt = promptSync();
    
    const result = prompt(message);