Using the popular node-fetch library, Intellisense doesn't understand the return value of fetch().
Vim with coc:
Same thing in VSCode:
For most other libraries I have type completion, but this one does not work.
jsconfig.json and tsconfig.json files@types/index.d.ts@types/node-fetch, which also falls out-of-date oftenimport syntax, which worksAs I mentioned, changing to import syntax fixes the issue, but I want it to work with require too.
How can I have type completion on this library with CommonJS?
const fetch = require('node-fetch');
fetch('https://github.com/')
.then(res => res.text())
.then(body => console.log(body));
This works no problem, have you checked the documentation?
This works for me:
import node_fetch = require("node-fetch");
const fetch = node_fetch.default;
fetch('https://github.com/')
.then(res => res.text())
.then(body => console.log(body));
(See https://www.typescriptlang.org/docs/handbook/modules.html)
Mind you, I think there must be a better way to get the default import...
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.