-
Notifications
You must be signed in to change notification settings - Fork 0
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
Added namespace to allow es6 style import #1
Conversation
This is not correct according to ES6 spec. If you import auth = require('basic-auth') this works with or import auth from 'basic-auth' this will work with |
Well a function is an object in JavaScript. import * as express from 'express';
const app = express(); The return value is a function. |
I said plain object. In ES6: import * as x from 'express'
console.log(typeof x) will log |
Compiling that code with Typescript, the console will log Will wait for |
Why can't you use one of the examples I gave you? |
@CaselIT Linked in that comment there's a good post by Ryan on Stack Overflow too: https://stackoverflow.com/questions/39415661/why-cant-i-import-a-class-or-function-with-import-as-x-from-y. I'd recommend bookmarking this for future use. |
I meant that to use the es6 style import will wait for the library to be updated. I resorted to the es5 style, but I dislike it because it'd different from almost all other imports I use. @blakeembrey thanks as always for the explanation. I had a feeling that the CommonJS import style in typescript was almost deprecated, or in any case the es6 style import was to be preferred (but that's just my impression, by seeing the es6 style almost everywhere, and is not supported by any official evidence so to speak). I personally find the es6 version more convenient in many cases. Just a curiosity, using module.exports = auth to module.exports = auth;
module.exports.default = auth; would this be acceptable as a way to use the es6 import Or it's more of an hack that should be avoided? |
@CaselIT Yes, I promote that synthetic default export a lot. I agree that the module.exports / export = / import = require() is ugly and not forward-compatible, but it is the only correct way |
@felixfbecker Very well. I'll make a pr to basic-auth to introduce it. Thanks for the clarification about how to best write type definitions. |
Have to jump in the discussion. Really need to use EDIT: the issue above relates to |
@unional If the commonjs library uses |
That should work; however, I would recommend not to do so because attempting to blur the line between CommonJS and ES2015 this way causes confusion to the team/user. It gives a wrong impression that interop can safely be used. My two cents. 🌷 |
I'm also 90% confident it would break when real ES6 modules are released. We would have to wait until the latest node.js ESM proposal is available though, it sounds like they may have changed/fixed some of these issues. There's not a lot going for a module author to add that code unless they are also controlling the consumption of |
@blakeembrey Why do you think that would break? Isn't the spec for the es6 modules final? |
Agree with Blake. You can check node-eps It is best to avoid interop and anything resemble it right now |
Es6 spec does not specify interop |
would suggest that the default import would continue to work, but not the namespace hack I was proposing to use in the PR. Also see the 3rd example |
@CaselIT The ES6 module spec is, yes, but even today you are not using that spec. No one supports it in production. You are using a transpiled version to CommonJS or AMD. |
@CaselIT That example which is quote is not compatible with this change. What TypeScript is doing when you do Edit: To clarify, actually, this code works accidentally still because of the fact that |
Note that the current |
Could be, but it's not so very clear. It should have been something like
Still since it's s draft, who knows what will actually be the implementation. And if ever there will be one |
Added empty package to allow es6 style import in typescript