I feel this is a question that should be answered but cannot find an answer.
I've switched my TS project to ESM. So my package.json now has "type": "module" and my tsconfig.json:
"target": "es2020",
"lib": ["es2020"],
"module": "node16",
"moduleResolution": "Node16",
"esModuleInterop": true, // Eases ESM support
"types": ["node"],
"allowSyntheticDefaultImports": true,
...
For the most part things work great but a few modules give me trouble. For example aedes and mqemitter and wait
import wait from 'wait'
await wait(1000)
The code above "works" but VSCode red underlines the "wait" in 2nd line w the error:
This expression is not callable.
Type 'typeof import(".../node_modules/wait/wait")' has no call signatures.
I have tried of course:
import * as wait from 'wait'
and
import { default as wait } from 'wait'
I have what I thought was the relevant ESM settings in my TS config.
I am on TS 4.8.3 and using the latest of all these troublesome packages.
Most of the posts I see just seem to talk about esModuleInterop, etc.
Adding .default such as wait.default() makes error go away but code won't run. Seemed to work here though.one
Any ideas?
UPDATE
Ok. The error was throwing me off a bit - I usually see VSCode telling me to install @types/... or declare which is what I did.
declare module wait
declare module aedes
declare module mqemitter
However I don't think this is the solution. I literally have to do this for 15-20 modules and some, like ajv which are fully done in TS should not need this as I would have to somehow modify their entire type definition - on a side note ajv because I call it with a contructor as new Ajv() yields a slightly diff error his expression is not constructable.. There are issues seemingly related but nothing super concrete as of yet.
Here' a sandbox.
For the troublesome modules such as wait and ajv in my case I imported them into a utils file then reexported with a TS type as follows:
import wait from 'wait'
export const pause = wait as unknown as typeof wait.default
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With