question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSDoc-typed node modules require special configuration in consumers to be useful

See original GitHub issue

It 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:open
  • Created 6 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
DavidWellscommented, Sep 24, 2019

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:

  1. My code is vanilla JS with JSdoc blocks for the types
  2. During my build process, I use tsd-jsdoc to convert the JSdocs from the source to a types.d.ts type file. https://github.com/DavidWells/analytics/blob/master/packages/analytics-core/package.json#L31
  3. In my package.json, I set the path to where typings lives https://github.com/DavidWells/analytics/blob/master/packages/analytics-core/package.json#L50

It 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!

2reactions
akdor1154commented, Oct 13, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found