-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inconsistent typechecking with require() in JS and TS #60032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Have you tried changing your tsconfig.json settings and using esModuleInterop?" |
Only
What specifically is the repro for this? |
Bug workbench doesn't seem to work with // @types: ["node"]
// @filename: test-cjs.ts
module.exports = { Value: 1 };
// @filename: main.ts
import test = require("./test-cjs");
test.Value; // Error: Property 'Value' does not exist on type 'typeof import("./test-cjs")'
// ^? any If the module has a JS extension, then the import works. But if the module has a TS extension, it no longer works.
Is it possible to add support for // Can't do this with import X = require()
let X;
if (cond) {
X = require("x-impl-1");
}
else {
X = require("x-impl-2");
} |
π Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript. Issue body code block by @u130b8 β Failed: -
Historical Information
|
π Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript. β Failed: -
Historical Information
|
π Search Terms
require import js ts module esm esmodule cjs commonjs
π Version & Regression Information
β― Playground Link
Multiple files not supported in playground, see bug workbench
π» Code
Workbench Repro
π Actual behavior
Type resolution is inconsistent when using
require()
from .js and .ts files:require()
in a .ts file is always unchecked (checked in a .js file or in .ts file withimport X =
syntax)π Expected behavior
I expected
require()
to be typechecked in .ts files because it's typechecked in .js files.I expected imports of CommonJS .ts files to work because CommonJS .js files work and are typechecked.
Additional information about the issue
This problem happens when porting an existing Node.JS codebase that uses CommonJS require() modules to TypeScript. It's not possible to port the code without also forcing it into ES Modules because:
The text was updated successfully, but these errors were encountered: