JSDoc-typed node modules require special configuration in consumers to be useful
See original GitHub issueIt looks like an ongoing goal for TS is to recommend JSDoc as the recommended way to give type-checking to raw JS users. As a user I am not a huge fan of this approach but I’ll try to be objective in this issue.
If I write a node module js-lib
as raw javascript typed with jsdoc, it will appear as untyped to consumers of my module unless they specifically opt in to JS type checking.
Example:
+-js-lib
| +-index.js
| +-package.json
|
+-ts-consumer
+-index.ts
+-package.json
+-tsconfig.json
js-lib/index.js
/**
* @param {string} s
* @returns {number}
*/
module.exports = function test(s) { return parseInt(s) };
js-lib/package.json
{
"name": "js-lib",
"main": "index.js"
}
ts-consumer/index.ts
import test = require('../js-lib');
// index.ts(1,23): error TS6143: Module '../js-lib' was resolved to '/Users/jarrad/src/personal/ts-test/lib/js-index.js', but '--allowJs' is not set.
import test = require('js-lib'); // after npm install ../js-lib
// index.ts(1,23): error TS7016: Could not find a declaration file for module 'lib'. '/Users/jarrad/src/personal/ts-test/lib/index.js' implicitly has an 'any' type.
// Try `npm install @types/lib` if it exists or add a new declaration (.d.ts) file containing `declare module 'lib';
test("test");
ts-consumer/tsconfig.json
{
"compilerOptions": {
"strict": true
}
}
It should be possible for me to make my js-lib
usable to Typescript authors without them having to configure their own project with allowJs
and checkJs
.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Requiring modules in Node.js: Everything you need to know
const config = require('/path/to/file');. The main object exported by the require module is a function (as used in the above example).
Read more >How To Use Node.js Modules with npm and package.json
Node.js organizes this complexity using modules, which are any single JavaScript files containing functions or objects that can be used by ...
Read more >CommonJS modules | Node.js v19.3.0 Documentation
CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and ......
Read more >How to Polyfill node core modules in webpack 5
I was also getting these error's when upgrading from webpack v4 to v5. Resolved by making the following changes to webpack.config.js.
Read more >Documentation - ECMAScript Modules in Node.js - TypeScript
certain global-like values like require() and __dirname cannot be used directly; CommonJS modules get imported under certain special rules. We'll come back to ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello there! Came here via https://twitter.com/orta/status/1176216539713953793
I recently added Typescript support to my project that is using JSdocs.
My setup is like this:
types.d.ts
type file. https://github.com/DavidWells/analytics/blob/master/packages/analytics-core/package.json#L31typings
lives https://github.com/DavidWells/analytics/blob/master/packages/analytics-core/package.json#L50It would be super awesome if TS automatically inferred the types from the JSdocs in my built source code. This would streamline things for me 😃
Anywho, thanks for all the great work on TS & the tooling around it. The autocompletion the package has is great!
What I am hinting towards is some way of me writing in my library’s
package.json
that Typescript should indeed dive into its JS files by default, or at least as far as it has to to type the externally visible interface. Strawman:"types": "[jsdoc]"
. This would (hopefully) void the performance concerns of a full node_modules JS-inference-spree.