Using the popular
node-fetch library, Intellisense doesn’t understand the return value of
Vim with coc:
Same thing in VSCode:
For most other libraries I have type completion, but this one does not work.
Things I tried
- Investigated the package, which exports a
- Tried installing
@types/node-fetch, which also falls out-of-date often
- Converted to
importsyntax, which works
As I mentioned, changing to
import syntax fixes the issue, but I want it to work with
How can I have type completion on this library with CommonJS?
const fetch = require('node-fetch').default
To bridge fundamental differences between CommonJS and ES module syntax,
require('node-fetch') returns an object with this shape:
Notice the node-fetch type definition does export the
fetch() function as the top level, but also exports a
default property for compatibility with ES module syntax. Using this property fixes autocomplete issues.
This still seems like a bug to me, but this workaround does resolve the issue.