Skip to content

Types from packages are not properly used when checking JS with esModuleInterop #35188

Closed
@stof

Description

@stof

TypeScript Version: 3.8.0-dev.20191119 (same result with 3.7.2 as well)

Search Terms: types checkJS

Code

{
  "dependencies": {
    "humane-js": "3.2.2"
  },
  "devDependencies": {
    "@types/humane-js": "3.2.0",
    "typescript": "3.8.0-dev.20191119"
  }
}
// typescript.ts
import humane from 'humane-js';

humane.timeout = 4000;

export const notifyInfo = humane.spawn({ addnCls: 'humane-info' });
// javascript.js (same code than typescript.ts)
import humane from 'humane-js';

humane.timeout = 4000;

export const notifyInfo = humane.spawn({ addnCls: 'humane-info' });

Expected behavior:

Running both tsc --noEmit --esModuleInterop typescript.ts and tsc --noEmit --esModuleInterop --allowJS --checkJS javascript.js succeed.

Actual behavior:

Type-checking for the Typescript file succeeds. But type-checking for the javascript files fails with this error:

javascript.js:5:34 - error TS2339: Property 'spawn' does not exist on type 'typeof humane'.

5 export const notifyInfo = humane.spawn({ addnCls: 'humane-info' });
                                   ~~~~~


Found 1 error.

Note that if I change my JS file to use a CommonJS require call rather than an ES import and then run tsc --noEmit --allowJS --checkJS javascript.js, tsc will properly identify the type of humane (type-checking succeeds on this code, and introducing a typo in the spawn method name gets caught)

It would be great if --esModuleInterop could work when using tsc to check JS code, to allow using it to check webpack projects (which have such interop loading of JS modules).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions