-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add command line flag to allow synthetic default exports #5577
Conversation
@@ -46,6 +46,7 @@ namespace ts { | |||
const compilerOptions = host.getCompilerOptions(); | |||
const languageVersion = compilerOptions.target || ScriptTarget.ES3; | |||
const modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; | |||
const allowSyntheticDefaultImports = compilerOptions.hasOwnProperty("allowSyntheticDefaultImports") ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System; |
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.
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
.
👍 |
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. |
Also related systemjs/systemjs#899 |
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. |
So @mhegazy, do we want to merge this? |
@@ -281,9 +281,14 @@ namespace ts { | |||
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file | |||
}, | |||
{ | |||
name: "allowSyntheticDefaultImports", | |||
type: "boolean", | |||
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export |
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.
looks good, if you can to update the flag message that would be great. |
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. |
Add command line flag to allow synthetic default exports
@weswigham can you add a blurb for this in https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#typescript-18-upcoming |
@mhegazy done. |
thanks! |
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 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 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. |
You should only use this flag iff your loader will generate a 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. |
@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? |
Have you tried |
so lets step back a bit, ES6 introduced this new concept of a the question is the if this is not true, you can still use the ES6 imports, you just can not use the if it still does not have the |
@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. @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 I will have another look tomorrow. Thanks for your help so far. |
As i mentioned earlier, 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. |
Some things work with 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 |
yes. |
Thanks, I really appreciate your help. |
Should be fixed with the --allowSyntheticDefaultImports flag, see microsoft/TypeScript#5577 But that's not been released yet so can't use it.
Should be fixed with the --allowSyntheticDefaultImports flag, see microsoft/TypeScript#5577 But that's not been released yet so can't use it.
Many npm modules use |
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 |
@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 |
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. |
@RyanCavanaugh I think what @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 @mhegazy said 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 |
@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. |
@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. |
@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. |
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 theexport=
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 withallowSyntheticDefaultImports: false
.