microsoft / microsoft/TypeScript-Sublime-Plugin

"Cannot find module" when requiring node_module with package.json types from .js but works fine in .ts

Offen
#768 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
JavaScript
Sterne
1.7k
Forks
236
Ø Merge
21 T. 13 Std.
Gemergte PRs (30 T.)
1

Beschreibung

I have some npm dependencies like socket.io or cheerio that provide type info for TS via the package.json types field.

This works just fine in a TS file such as server.ts:

import socketIO = require('socket.io')
socketIO.blah // correctly warns about property not existing
socketIO.Socket // correctly knows this exists

In javascript files however I get en error on the require line, ex in server.js:

// @ts-check
const socketIO = require('socket.io') // "Cannot find module 'socket.io' or its corresponding type declarations"

I did try and add a reference path comment/hint but that didn't work in server.js:

// @ts-check
/// <reference path='./node_modules/socket.io/dist/index.d.ts' />
const socketIO = require('socket.io') // "Cannot find module 'socket.io' or its corresponding type declarations"

However, type info comes through just fine for anything I've had to use DefinitelyTyped for. So for example if I have express as a dependency and @types/express as a devDependency then this works in server.js:

// @ts-check
const express = require('express')
express.blah // correctly warns about property not existing
express.static // correctly knows this exists

Seems like something is missing here when it comes to reading the types field off of package.json for dependencies. Is this just broken/missing functionality for javascript files or am I just missing some kind configuration or something?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

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 in server.js mit @ts-check und einer socket.io-Abhängigkeit, deren package.json ein types-Feld enthält, und vergleiche anschließend das Ergebnis mit server.ts und dem express/@types/express-Beispiel. Verfolge zunächst, wie das Plugin den require und den package.json-types-Eintrag auflöst; fertig ist die Aufgabe, wenn JavaScript-Imports die gebündelten Deklarationen auflösen und die gemeldete Eigenschaftsprüfung beibehalten.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, node.js, typescript
Bereich
developer-experience, tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
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.