microsoft / microsoft/TypeScript

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

Offen
#19,145 10 Kommentare 11 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Problem mit js-lib/index.js, js-lib/package.json, ts-consumer/index.ts und ts-consumer/tsconfig.json aus dem Beispiel. Beginne damit nachzuverfolgen, wie der Compiler ein JavaScript-Node-Modul auflöst und Typinformationen für einen TypeScript-Consumer entdeckt. Fertig ist die Arbeit, wenn ein mit JSDoc typisiertes Modul nützliche Typen bereitstellen kann, ohne dass Consumer allowJs und checkJs aktivieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, node.js, typescript
Bereich
compilers, developer-experience, tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.