Open u130b8 opened 1 month ago
Have you tried changing your tsconfig.json settings and using esModuleInterop?"
I expected require() to be typechecked in .ts files because it's typechecked in .js files.
Only import i = require(
is supported in TS
I expected imports of CommonJS .ts files to work because CommonJS .js files work and are typechecked.
What specifically is the repro for this?
Bug workbench doesn't seem to work with types: ["node"]
, but you can repro it like this:
// @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.
Only
import i = require(
is supported in TS
Is it possible to add support for const i = require(
as well?
It seems strange because the functionality is there, the const require works and gets typechecked, but only with a JS extension, why not have it work if the file extension is TS as well? It would make the developer experience much better, and const require is much more flexible than import require:
// Can't do this with import X = require()
let X;
if (cond) {
X = require("x-impl-1");
}
else {
X = require("x-impl-2");
}
:wave: 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
:x: Failed: -
Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
File '/home/runner/work/TypeScript/TypeScript/module-cjs-ts.ts' is not a module.
File '/home/runner/work/TypeScript/TypeScript/module-cjs-ts.ts' is not a module.
Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
File '/home/runner/work/TypeScript/TypeScript/module-cjs-ts.ts' is not a module.
Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
File '/home/runner/work/TypeScript/TypeScript/module-cjs-ts.ts' is not a module.
Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
Version | Reproduction Outputs |
---|---|
5.2.2, 5.3.2, 5.4.2, 5.5.2, 5.6.2 |
:x: Failed: -
|
:wave: 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.
Comment by @u130b8
:x: Failed: -
Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
File '/home/runner/work/TypeScript/TypeScript/test-cjs.ts' is not a module.
Version | Reproduction Outputs |
---|---|
5.2.2, 5.3.2, 5.4.2, 5.5.2, 5.6.2 |
:x: Failed: -
|
π 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: