`esModuleInterop: true` cause runtime error

Offen
#41,898 13 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@rbuckton arbeitet bereits daran.

Seit 14.12.2020.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Beschreibung

Needs Investigation

Hi there, problems happened when I toggled esModuleInterop flag for my project, I have to because one library it depends enabled this flag.

TypeScript Version: 2.7+

Search Terms:
esModuleInterop
allowSyntheticDefaultImports

Code

import * as apmStar from 'elastic-apm-node'
import SentryDefault from '@sentry/node'

apmStar.start // undefined
SentryDefault // undefined

Explains:

  • for @sentry/node it is an es module, so esModuleInterop has no effects on it, but with allowSyntheticDefaultImports you can import its default, which property it does not export, so you got an undefined error at runtime
  • for elastic-apm-node, it is a commonjs module, and it exports an instance, start is a prototype method of it, so it is lost after __importStar.

But these problems are not informed in the document, guess we could discourage enabling it for library projects?
And I wonder if we could improve type checking for these situations, for example:

  • for apmStar, it is imported as esmodule namespace, so an type error could be thrown when accessing .start method on it.
  • for SentryDefault, it is a esmodule, allowSyntheticDefaultImports could be disabled for it

Expected behavior:
Error emitted at compiling time
Actual behavior:
Compiled successfully, but got an error at runtime.

Playground Link:
https://repl.it/@themez1/esModuleInteropTest

Related Issues:
https://github.com/microsoft/TypeScript/issues/28009
https://github.com/microsoft/TypeScript/issues/33954
https://github.com/microsoft/TypeScript/issues/36026

Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microsoft/TypeScript

Alle Issues in microsoft/TypeScript

Ähnliche Issues

Weitere Issues zu Go

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.