Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

mongodb - Does typescript automatically bundle all exports as default?

import * as mongodbModule from 'mongodb';

The above code is the standard es6 way to import mongodb in nodejs, doing it like so import mongodbModule from 'mongodb' is supposed to throw an error because the mongodb module has no default export; but it still works. I discovered that the mongodb module is a typescript file hence my question.

question from:https://stackoverflow.com/questions/65927073/does-typescript-automatically-bundle-all-exports-as-default

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Whether or not you can write import mongodbModule from 'mongodb' without getting an error depends on your configuration in .tsconfig. In order to enable this, you use the setting --allowSyntheticDefaultImports. As explained in the docs, it's not really that typescript creates the bundle, it's more that typescript assumes that the bundle exists and allows you to import it, while babel handles the actual creation of it.

This flag does not affect the JavaScript emitted by TypeScript, it only for the type checking. This option brings the behavior of TypeScript in-line with Babel, where extra code is emitted to make using a default export of a module more ergonomic. ... For convenience, transpilers like Babel will automatically create a default if one isn’t created.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...