Skip to content

TypeScript generated invalid types if esModuleInterop is enabled #52779

Open

Description

Bug Report

When a package is typed using module.exports =, the correct way to type it is export =. The correct way to import this, in either .ts or .d.ts files, is import identifier = require('module'). For example import React = require('react').

If a user enables esModuleInterop, they are allowed to use import React from 'react'. When compiling to CJS, this will output JavaScript that used either module.exports.default or module.exports.

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));

IMO This isn’t great, but it works.

What is bad, is that it generates a default import in the type definitions.

import React from 'react';

Whereas it should be typed as this:

import React = require('react');

If this is library code, that means the user of the library now is also enforced to use esModuleInterop, whether they like it or not.

🔎 Search Terms

esModuleInterop

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs

⏯ Playground Link

Playground link with relevant code (Look at the .d.ts output)

💻 Code

The following code requires esModuleInterop to be enabled.

import React from 'react'

export const element = React.createElement('div')

🙁 Actual behavior

This is the generated type definition:

import React from 'react';
export declare const element: React.DetailedReactHTMLElement<React.HTMLAttributes<HTMLElement>, HTMLElement>;

🙂 Expected behavior

This is the correct type definition:

import React = require('react');
export declare const element: React.DetailedReactHTMLElement<React.HTMLAttributes<HTMLElement>, HTMLElement>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions