如果esModuleInterop为true,那么我是否需要显式的allowSyntheticDefaultImports配置TypeScript转换? [英] Do I ever need explicit allowSyntheticDefaultImports if esModuleInterop is true configuring TypeScript transpilation?

查看:2252
本文介绍了如果esModuleInterop为true,那么我是否需要显式的allowSyntheticDefaultImports配置TypeScript转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确认以下理论。根据 TS文档,有两个选项可以在 tsconfig.json

I need confirmation on the following theory. According to TS docs, there are two options that can be set in tsconfig.json.


  1. - allowSyntheticDefaultImports:允许默认从没有默认导出的模块导入。这不会影响代码发出,只会影响类型检查。

  1. --allowSyntheticDefaultImports: Allow default imports from modules with no default export. This does not affect code emit, just typechecking.

- esModuleInterop: Emit __importStar和__importDefault运行时babel生态系统兼容性的助手,并启用--allowSyntheticDefaultImports用于类型系统兼容性。

--esModuleInterop: Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility.

当我google时,我看到两者都被设置为 true (至少在我所针对的行为方面)。但是,到目前为止,我理解文档,TS和转换为JS,使用它们都没有意义。

When I google around, I see both being set to true (at least in regard to the behavior I'm aiming at). However, as far I understand the docs, TS and transpilation to JS, it makes no sense to use them both.

我想的方式,我可能只使用后者并完全删除前者。然而,谨慎和谦虚,我并不完全确定并担心我现在做的事情不那么明显而没有意识到。

The way I figure, I might use the latter only and entirely remove the former. However, being cautious and humble, I'm not entirely certain and worry that I'm doing something less bright without realizing it at the moment.

我担心这是什么不合适的,以后会在驴子里咬我,导致数小时的悲伤和拉扯,同时拼命地捣乱。怀疑主义的基础是两个选项都可用,所以我推断有四种情况需要所有组合(真/假等),但我无法想象它们是哪种是

I fear that it's something inappropriate that's going to bite me in the donkey later on causing hours of lamenting and hair-pulling while desperately trouble-shooting. The basis for the skepticism is that both options are available, so I'm inferring that there are four cases where all the combinations (true/false etc.) are required but I can't imagine which they are.

如果中的 - esModuleInterop:true ,跳过 - allowSyntheticDefaultImports 是完全安全的吗? compilerOptions ?如果是这样,为什么我们有这个选项?

Is it entirely safe to skip --allowSyntheticDefaultImports if --esModuleInterop: true in compilerOptions? ANd if so, why do we have that option?

奖金问题:所有四种组合何时需要( true / false )对于这两个选项?

Bonus question: when is it required with all the four combinations (true/false) for those two options?

推荐答案

如果你的意思是你可以离开 allowSyntheticDefaultImports 未定义并仅定义 esModuleInterop ,答案应该是YES继续前进,但是存在此问题。 PR#26866 似乎是一个解决方案,仅在9月17日合并,所以它可能在短期内存在一些风险。

If you mean can you leave allowSyntheticDefaultImports undefined and define only esModuleInterop, the answer should be YES moving forward, but there has been an issue with this. PR #26866 seems to be a fix, only merged September 17, so it there may be some risk in the short term.

为什么两者都存在,我相信这些都是解决与Babel转换模块导入的兼容性问题的一部分,原始PR为某些编译添加了allowSyntheticDefaultImports选项 - 时间消息,但实际上没有解决导入的运行时行为。所以--esModuleInterop后来添加了。有关如何更新文档的讨论,请参见 TypeScript-Handbook /#816 。 。

As why both exist, I believe these were both a part of addressing compatibility issues with imports of Babel-transpiled modules, the original PR added the allowSyntheticDefaultImports option to certain compile-time messages, but in practice didn't address the runtime behavior of the imports. So --esModuleInterop was added later. See TypeScript-Handbook/#816 for discussion of how to update the docs...

这篇关于如果esModuleInterop为true,那么我是否需要显式的allowSyntheticDefaultImports配置TypeScript转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆