-
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
Implicit module import/export elision #2812
Comments
A few days ago, I created a file like: /// Thenable.ts
interface Thenable<T> {
// then() method definition
}
export default Thenable; Then in another: /// foo.ts
import Thenable from "./Thenable";
var p: Thenable = ...; I wouldn't be against your suggestion to change BTW, isn't requireing the "type" keyword for importing types also already a breaking change, given that people seem to be using 1.5.0-alpha a lot already? |
yes that would be a breaking change. marking it. |
Agree about export default not having a value, instead you can write:
Symmetry on import:
IMHO, both 'import interface' and 'import type' make it more intuitive. |
@jon Yeah, I agree with Given that the ES6 committee has really tried hard to make default imports/exports easy to use, I expect them to be used a lot. So, imports will often look like: import Foo from "./Foo";
import Bar from "./Bar";
import type Baz from "./Baz";
// etc. To me, that looks cleaner than: import Foo from "./Foo";
import Bar from "./Bar";
import type { Baz } from "./baz"; Syntax for the default export could e.g. look like: interface Baz { ... }
export default type Baz; @mhegazy I'm probably missing something very obvious here, can you comment on why you specifically want to prohibit default type exports? Would the following still be allowed? interface Baz { ... }
export { type Baz as default }; Or even import { type default as Baz } from "./Baz"; |
For me it's more about not having too much syntax for importing. This seems enough:
Are these really needed? Adds visibility clutter if people use in different ways:
For the 'import interface' that's just a personal preference for symmetry: |
As @jbondc explained; this was the intention; trying to limit the additional syntax to minimum, in theory we can make default take a |
@mhegazy Feared as much, which is why I asked whether the 'alternative' default syntax would be allowed. Thanks for the confirmation that it's indeed 'just' the additional syntax. Given ES6's 'push' for the default syntax, as a user, I think it would be worth the trouble to add the 'entry to the matrix', but then again, I don't feel the pain of adding/maintaining it in the compiler ;) @jbondc As @mhegazy stated in his proposal, the |
my argument is if you have a value, then there is no issue, which is the common case (i.e. class, function, var, module). it only breaks down if you only have an interface or a type alias, which really does not exist in the .js anyways.. so for these you can do the slightly longer version of |
Summary: It will be an error to re-export a type-only identifier from an import in ES6 in |
@RyanCavanaugh Do you mean that the outcome of other discussions have basically obsoleted/replaced all of the OP's proposal, and this is the only part that remains? We'd like to know, because we would like to start using ES6-style imports, but are holding off until this settles. |
yes. this is correct, there are no intended changes/additions to the module syntax. the reaming work from this suggestion is to flag an error when using |
Nice, thanks! |
Sounds like we're not doing any other work on this issue. Is that correcy @mhegazy ? |
yup. this was declined last time we talked about it. |
The rational is that the import syntax is already complex enough, and no need to add more complexity. |
Current behavior
The compiler today elides imports and exports that are types or not referenced in a value position in the body of the importing module. See spec sections section 11.2.5 and section 11.2.6 for more details.
This has allowed for using the same syntax to import types and/or namespaces the same way values are imported. This has been convenient but has caused some problems:
export { t } from "mod"
) should be elided or not without looking at the whole programProposed change
1. Do not elide module imports even if they are only used in type position.
Even if the import is not used, keep the module dependency (i.e. the require() call) to ensure any side effects are maintained.
e.g.:
emits in ES6:
and ES5/commonjs:
2. Exports with no value side are always elided
This is the existing behavior, an export to an interface or a non-instantiated module will be elided.
e.g.:
3. A
type
modifier is required for type-only imports and exportsThe
type
modifier will cause the import to alias the type side of the imported entity, and would indicate the intent for this import statement to be always elided.Optionally
type
can be applied to specific named bindings in export and import declarations, which will result in eliding the import if all its bindings are types.Errors:
type
on a value-only import/export (e.g. var declaration).type
modifierdefault
exports must have a value side:Implications to
import =
/export =
syntaxThe proposal only affects the new ES6-style import/export syntax. Existing elision rules for
import id = require("module")
andexport = id
are not changed.The text was updated successfully, but these errors were encountered: