Skip to content

In JS, checking require.main === module causes the type of module to get messed up #41764

@AlCalzone

Description

@AlCalzone

TypeScript Version: 4.1.2

Search Terms: require.main module

When using Node.js's recommended approach to check if a module is the main module, tsc chokes on the types it infers:

Code
Note: This is JavaScript!

if (require.main === module) { // ERROR TS2367 The types "Module | undefined" and "{ "\"c:/Repositories/ioBroker.test/repro\"": number; }" have no overlap
	module.exports = 1; // ERROR TS2339 The property exports does not exist on type `never`
}

Expected behavior:
no error

Actual behavior:
bunch of incorrect errors (see code). I'm not even sure what that type which is inferred for module is supposed to mean.

Playground Link: doesn't work on the playground

Related Issues: #30088

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions