GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add command line flag to allow synthetic default exports by weswigham · Pull Request #5577 · microsoft/TypeScript

Conversation

@weswigham

@weswigham Wesley Wigham (weswigham) commented Nov 9, 2015

Copy link
Copy Markdown
Member

From #5285.

When --allowSyntheticDefaultImports is specified, it indicates that the module loader performs some kind of synthetic default import member creation not indicated in the imported .ts or .d.ts. We infer that the default member is either the export= member of the imported module or the entire module itself should that not be present. system has this flag on by default, however it can be overridden with allowSyntheticDefaultImports: false.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ethanresnick reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@weswigham
Add command line flag to allow synthetic default exports
a32f44f
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-already-signed label Nov 9, 2015
mhegazy
mhegazy reviewed Nov 9, 2015
View reviewed changes
Comment thread src/compiler/checker.ts Outdated

@mhegazy Mohamed Hegazy (mhegazy) Nov 9, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about compilerOptions.allowSyntheticDefaultImports !== undefined or typeof compilerOptions.allowSyntheticDefaultImports !== "undefined"?

i am just concerned about readability, and refactor/rename support in tooling.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@weswigham Wesley Wigham (weswigham) Nov 9, 2015

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair! I'll use typeof.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhegazy

Mohamed Hegazy (mhegazy) commented Nov 9, 2015

Copy link
Copy Markdown
Contributor

👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Nov 9, 2015

Copy link
Copy Markdown
Contributor

One thing to note, with Babel dropping support for export default magic: babel/babel#2212, do we still need this? i would expect systmjs/es6_module_loader to follow suite.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham
use typeof
9024032
@myitcv

Paul Jolly (myitcv) commented Nov 10, 2015

Copy link
Copy Markdown

Also related systemjs/systemjs#899

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahejlsberg

Anders Hejlsberg (ahejlsberg) commented Nov 10, 2015

Copy link
Copy Markdown
Member

My understanding is that Babel will be removing it on the export side, but will be keeping the magic on the import side. SystemJS likewise has magic lifting on the import side. So, unless both Babel and SystemJS both drop the magic on the import side, we still need the flag.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Nov 12, 2015

Copy link
Copy Markdown
Member Author

So Mohamed Hegazy (@mhegazy), do we want to merge this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Nov 13, 2015
Transpilation of ES6 imports is inconsistent with babel #5458
Closed
@ahejlsberg Anders Hejlsberg (ahejlsberg) mentioned this pull request Nov 16, 2015
Importing momentjs using jspm #5682
Closed
@weswigham
Merge branch 'master' into autohoist-default
f28958f
mhegazy
mhegazy reviewed Nov 26, 2015
View reviewed changes
Comment thread src/compiler/commandLineParser.ts Outdated

@mhegazy Mohamed Hegazy (mhegazy) Nov 26, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add more description that we are not adding anything to the emitted code, but that it is just an illusion to match what the loader would do.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhegazy

Mohamed Hegazy (mhegazy) commented Nov 26, 2015

Copy link
Copy Markdown
Contributor

looks good, if you can to update the flag message that would be great.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Nov 26, 2015

Copy link
Copy Markdown
Contributor

We talked about making it an error to call the namespace import if this flag is on, as es6-module-loader copies the properties on it at run time, but not the prototype. this is however dependent on the implementation of the loader, it is possible that the loader changes the implementation to allow both to be callable.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Wesley Wigham (weswigham) added 3 commits November 25, 2015 17:27
@weswigham
update description
adc3f2b
@weswigham
Merge branch 'master' into autohoist-default
d74ab2e
@weswigham
accept new baselines
fd5f440
Wesley Wigham (weswigham) added a commit that referenced this pull request Nov 26, 2015
@weswigham
Merge pull request #5577 from weswigham/autohoist-default
38215c6
Add command line flag to allow synthetic default exports
@weswigham
Wesley Wigham (weswigham) merged commit 38215c6 into microsoft:master Nov 26, 2015
@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

Wesley Wigham (@weswigham) can you add a blurb for this in https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#typescript-18-upcoming

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Dec 1, 2015
SystemJS and default import with export = proposal #5285
Closed
@weswigham

Wesley Wigham (weswigham) commented Dec 1, 2015

Copy link
Copy Markdown
Member Author

Mohamed Hegazy (@mhegazy) done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glen-84 Glen (glen-84) mentioned this pull request Dec 1, 2015
Add "allowSyntheticDefaultImports" compiler option TypeStrong/atom-typescript#762
Closed
@glen-84

Glen (glen-84) commented Dec 1, 2015

Copy link
Copy Markdown

Is this the correct option to use when you want to import modules using the ES6 syntax where the definitions of the modules use exports in the form export = x?

For example, my code is targeting ES6 and I want to make use of the type definitions for Gulp. If I use:

import gulp from "gulp";

I get Module '"gulp"' has no default export.. However, if I set allowSyntheticDefaultImports to true, the error goes away.

I just want to make sure that this is one of the intended uses, as I don't quite understand the option's description.

Thanks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

You should only use this flag iff your loader will generate a default export for you if one does not exisit, e.g. using systemJs. All the flag does, it is mimics what SystemJs does at run time by creating a fake default export on modules that have the same shape as the module.

if you are not using system JS, and your loader does not do any of these tricks, e.g. AMD or commonJs do not use the flag.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glen-84

Glen (glen-84) commented Dec 1, 2015

Copy link
Copy Markdown

Mohamed Hegazy (@mhegazy) D'oh. I'm targeting ES6 which is then being loaded by Gulp (via Babel [gulp.babel.js]).

As I mentioned here, I'm unsure as to what my options are. I'd like to use ES6 module import syntax, but then the existing type definitions (on DefinitelyTyped) will not work. Is there any guidance in this regard?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@weswigham

Wesley Wigham (weswigham) commented Dec 1, 2015

Copy link
Copy Markdown
Member Author

Have you tried import * as gulp from "gulp"? While in ES6 this technically shouldn't be callable/constructable, with many dts files and commonjs modules, it transpiles to the right thing (var gulp = require("gulp");) and we don't emit errors on constructing/calling it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

so lets step back a bit, ES6 introduced this new concept of a defualt export. it is a special property on the module. since ES6 modules are not supported by any engine, then there is a pre-ES6 implementation that you are using.

the question is the gulp module at runtime does not have a default property on it. if this is the case, then the fix is to update the typings to reflect that, and you can then import it as import gulp from "gulp";.

if this is not true, you can still use the ES6 imports, you just can not use the default import, i.e. import * as gulp from "gulp".

if it still does not have the default export but your loader adds the it for you, then use this flag to avoid errors at compile time.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glen-84

Glen (glen-84) commented Dec 1, 2015

Copy link
Copy Markdown

Wesley Wigham (@weswigham) I'm not at my PC at the moment, but IIRC, that syntax worked for some modules but not others, which was messy, and not really what I wanted in the first place. I will confirm this tomorrow.

Mohamed Hegazy (@mhegazy) Gulp was a bad example, since I'm actually switching to Gulp 4 which doesn't have any typings yet, but let's just pretend that I'm still using Gulp 3.x for now (because this applies to other modules as well): If I updated the typings from export = x to export default x, wouldn't this break existing code that uses the old-style import x = require("x") imports?

I will have another look tomorrow. Thanks for your help so far.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 1, 2015

Copy link
Copy Markdown
Contributor

import d from "module" imports a property named default from that module.
import * as n from "module" imports all properties from the module. this is the closest equivalent to your import n = require("module").

As i mentioned earlier, default is a new thing in ES6, your existing node modules will probably not have a default export. some loaders, like SystemJs fake that by creating a fake default property on the modules on import if they do not have one.

so in short, if you want to use default, first check the module.js file if it exports a default, check the typing to verify they are accurate, and understand your loader. hope that helps.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glen-84

Glen (glen-84) commented Dec 2, 2015

Copy link
Copy Markdown

Mohamed Hegazy (@mhegazy),

Some things work with * as x, and some things don't (f.e. modules that export unnamed functions or object literals). It also changes the usage of the module, because you're essentially prefixing the exports, if I'm not mistaken. So, this is messy.

When I use Babel, I can import all these different types of exports in the same consistent ES6 style. I'm assuming that Babel adds the default export during import, so does this mean that it is in fact appropriate for me to use allowSyntheticDefaultImports, where the synthetic default import is done by Babel during run time?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Dec 2, 2015

Copy link
Copy Markdown
Contributor

yes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@glen-84

Glen (glen-84) commented Dec 2, 2015

Copy link
Copy Markdown

Thanks, I really appreciate your help.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Paul Lessing (paullessing) added a commit to paullessing/dnd-character-sheet2 that referenced this pull request Jan 11, 2016
@paullessing
Workaround for using moment
6b2d1a9
Should be fixed with the --allowSyntheticDefaultImports flag, see microsoft/TypeScript#5577
But that's not been released yet so can't use it.
Paul Lessing (paullessing) added a commit to paullessing/dnd-character-sheet2 that referenced this pull request Jan 11, 2016
@paullessing
Workaround for using moment
6cf7aa1
Should be fixed with the --allowSyntheticDefaultImports flag, see microsoft/TypeScript#5577
But that's not been released yet so can't use it.
@nycdotnet Steve Ognibene (nycdotnet) mentioned this pull request Feb 26, 2016
add default export so it can be used with es6 modules DefinitelyTyped/DefinitelyTyped#8250
Closed
@felixfbecker

Felix Becker (felixfbecker) commented Mar 9, 2016

Copy link
Copy Markdown
Contributor

Many npm modules use module.exports = which in ES6 is equivalent to import x from 'y' (default import). import * as x from 'y' is something completely different, it results in a plain object that cannot be a function for example. So it would be really nice if TS allowed to use default imports for modules that use module.exports = and exports = typing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@billti

Bill Ticehurst (billti) commented Mar 10, 2016

Copy link
Copy Markdown
Member

You need a loader which loads such CommonJS modules, and creates the expected default property, for this to work at runtime. SystemJS does that, and the --allowSyntheticDefaultImports flag added in this pull request is on by default if your module type is SystemJS.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@felixfbecker

Felix Becker (felixfbecker) commented Mar 10, 2016

Copy link
Copy Markdown
Contributor

Bill Ticehurst (@billti) I would like to use this with NodeJS. When transpiling with Babel, Babel adds this. I would like a flag to tell TS to do it too, because I don't like the import x = require('y') syntax

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Mar 10, 2016

Copy link
Copy Markdown
Member

I don't understand what the goal of using ES6 syntax is if you're going to use it to do things it explicitly does not support? It just means if you ever do move to a real ES6 module runtime, you're going to be broken in nonobvious ways.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@delebash

Daniel Elebash (delebash) commented Mar 28, 2016

Copy link
Copy Markdown

Ryan Cavanaugh (@RyanCavanaugh) I think what Felix Becker (@felixfbecker) and I also want is just the syntax. For example if I want to use jquery currently I can if I use import $ = require('jquery');. Instead I would just like to use the ES6 syntax of import $ from 'jquery'. I don't want to have type require for libs that don't have a defualt export and ES6 import syntax for those libs that do export a default. I currently use both ES6 import syntax and require syntax in as single file for importing different libs.

As Mohamed Hegazy (@mhegazy) said
import d from "module" imports a property named default from that module.
import * as n from "module" imports all properties from the module. this is the closest equivalent to your import n = require("module").

I would like typescript to look at a lib such as jquery determine if it does not have a default export and translate my import $ from 'jquery' to import * as $ from 'jquery' or import $ = require('jquery') which every is more appropriate

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@felixfbecker

Felix Becker (felixfbecker) commented Mar 28, 2016

Copy link
Copy Markdown
Contributor

Daniel Elebash (@delebash) I now use target: ES6 and then compile with Babel. Works perfectly with import default, in fact, import = require() is not even allowed with target: ES6.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@delebash

Daniel Elebash (delebash) commented Mar 28, 2016

Copy link
Copy Markdown

Felix Becker (@felixfbecker) Yes this works, but one of the points of Typescript is not having to rely on babel or any other compiler and being able compile/convert it's unique code style to whatever js version and module format that you specify in the tsconfig. This should not change just because we have an new version of javascript, Javascript will continue to evovle ES6,7,8 and Typescript needs to support compiling to past and present versions of js.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@felixfbecker

Felix Becker (felixfbecker) commented Mar 28, 2016

Copy link
Copy Markdown
Contributor

Daniel Elebash (@delebash) I would argue with that, the main reason I use Babel is because TypeScript only allows async/wait for ES6 but ES6 will not transpile stuff that NodeJS does not support yet, default params for example. In Babel however I can use a preset that fits exactly what I need to transpile.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@texastoland Texas Toland (texastoland) mentioned this pull request Apr 23, 2016
ES6 Modules #2242
Closed
@johnnyreilly John Reilly (johnnyreilly) mentioned this pull request May 6, 2016
How can I import momentjs in es6? moment/moment#2608
Closed
@mxl Michael Ledin (mxl) mentioned this pull request Jul 7, 2016
I18next: reverting default export DefinitelyTyped/DefinitelyTyped#10006
Merged
@guybedford Guy Bedford (guybedford) mentioned this pull request Aug 3, 2016
angular-cli with SystemJS integration getsentry/sentry-javascript#671
Closed
@keawade Keith Wade (keawade) mentioned this pull request Nov 1, 2016
Update auth0-lock to match default export DefinitelyTyped/DefinitelyTyped#12315
Merged
@PendletonJones PendletonJones mentioned this pull request Jun 10, 2017
Importing default module fails when using ES6 syntax #3337
Closed
@weswigham
Wesley Wigham (weswigham) deleted the autohoist-default branch August 17, 2017 23:04
@HerringtonDarkholme Herrington Darkholme (HerringtonDarkholme) mentioned this pull request Oct 15, 2017
Missing default export when using SystemJS or RequireJS makes Vue unusable with TypeScript vuejs/vue#6815
Closed
This was referenced Jun 5, 2018
TS 2.9 follow up vega/vega-lite#3839
Merged
Pretending json-stable-stringify has an es6 default export causes errors compiling in Typescript vega/vega-lite#3855
Closed
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

10 participants

@weswigham @mhegazy @myitcv @ahejlsberg @glen-84 @felixfbecker @billti @RyanCavanaugh @delebash @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.