microsoft / microsoft/TypeScript
`esModuleInterop: true` cause runtime error
@rbuckton ya está trabajando en esto.
Desde el 14/12/2020.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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/nodeit is an es module, soesModuleInterophas no effects on it, but withallowSyntheticDefaultImportsyou 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,startis 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.startmethod on it. - for
SentryDefault, it is a esmodule,allowSyntheticDefaultImportscould 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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Evaluación
Este issue todavía no se ha evaluado.