-
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
Types are not imported properly for packages that use exports.default. #25854
Comments
@zavr-1 This seems to be working for me. |
@zavr-1 So what do you see when hovering over EDIT: Never mind, got a repro after deleting |
OK, reproduced this bug. It happens because
@weswigham Could you take a look? |
A synthetic default always overrides a real one unless the |
For reference, if you edit the output to have an /// <reference path='fourslash.ts'/>
// @allowJs: true
// @allowSyntheticDefaultImports: true
// @Filename: /a.js
////exports.__esModule = true;
////exports.default = f;
/////**
//// * Run this function
//// * @param {string} t
//// */
////function f(t) {}
// @Filename: /b.js
////import f from "./a"
/////**/f
verify.quickInfoAt("", `(alias) (property) f: (t: string) => void
import f`, "Run this function"); // Passes |
We should probably recognize Object.defineProperty(exports, 'name', {...}); at the top level of a file as a special assignment, since our emit when targeting es5 and babel's current default emit now use such a call for the marker over a straight assignment. @sandersn You know how doable this is? |
@zavr-1 As a workaround, if you use babel with the |
@weswigham alright thanks, the |
@weswigham I have seen this pattern around and agree that it would be useful to have. I can't tell from this discussion whether it needs a new issue to track it or not. Do you think so? If not, we should change the title to say exactly that. Adding the pattern is not too difficult, but a lot more wordy than existing patterns. You'd have to
I would note that roundtripping our emit is not a goal and I am tempted to make it a non-goal. I'm not convinced roundtripping Babel's emit should be a goal either, but humans also write this pattern, so I'd class it as Nice to Have right now. |
We can make this issue track it - we probably need to edit the title and the OP a bit to make the lede not burried in the comments, however. |
@weswigham it doesn't actually work even if you set exports.__esModule
exports.default = testFunction
/**
* Run this function
* @param {string} t
*/
function testFunction(t) {
return t
} What you have done in your fourslashes is named function |
Hm. In our test harness, this: /// <reference path='fourslash.ts'/>
// @allowJs: true
// @allowSyntheticDefaultImports: true
// @Filename: /a.js
////exports.__esModule = true;
////exports.default = f;
/////**
//// * Run this function
//// * @param {string} t
//// */
////function f(t) {}
// @Filename: /b.js
////import q from "./a"
/////**/q
verify.quickInfoAt("", `(alias) (property) q: (t: string) => void
import q`, "Run this function"); // Passes Still works fine (though I've only checked on latest |
Ahhh @zavr-1 your example seems to be missing an actual assignment to |
TypeScript Version: 3.1.0-dev
Search Terms: type export default
Code and Actual Behaviour
Scenario 1 (perfect):
export default
,import alias
tt.js
t.js
Scenario 2 (Bug):
exports.default
,import originalName
tt.js
(compiled w/ babel)t.js
There's no description on hover, but it comes up when starting to type arguments. But this is only because it's got the same name so VS Code looks up by the name. Proof: if function is not exported, it will still show description. Therefore, same as case 3.
Scenario 3 (Bug):
exports.default
,import alias
tt.js
(compiled w/ babel)t.js
Expected behavior: Should always work as scenario 1.
So it basically means that all modules transpiled with babel must be
import
ed using their original name? And even then it's only because it has got the same name.Consider having 2 files:
tt.js
and
tt2.js
and a file in which I want to use them:
t.js
I won't be able to ever see type of
testFunction2
even usingtypeof {import}
? Unless I domodule.exports = testFunction
intt2.js
which Babel never does (and there's no setting for it). I think TypeScript should parseexports.default =
in the same way asmodule.exports =
asbabel@5
did export then this way, whereasbabel@6
doesn't.https://github.com/59naga/babel-plugin-add-module-exports#readme
I can't even use the above plugin because even though it adds
module.exports = exports.default;
VS Code does not assign the type tomodule.exports
.The text was updated successfully, but these errors were encountered: