microsoft / microsoft/TypeScript

`esModuleInterop: true` cause runtime error

オープン
#41,898 コメント 13 件 リアクション 1 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2020年12月14日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。