Skip to content
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

Simultaneous use of export equals and export default in a module #10854

Closed
tinganho opened this issue Sep 11, 2016 · 5 comments
Closed

Simultaneous use of export equals and export default in a module #10854

tinganho opened this issue Sep 11, 2016 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@tinganho
Copy link
Contributor

I have some NPM modules written that uses the old default export syntax:

declare module 'my-module' {
    function test(): string;
    export = test;
}

To promote ES import syntax but still keep the old one, I wanted to add both the new and old default export syntax:

declare module 'my-module' {
    function test(): string;
    export default test;
    export = test; // An export assignment cannot be used in a module with other exported element
}

Though it complains that it cannot use an export assignment with other export statements.

I think it is reasonable to support both syntax simultaneously in a module. Because choosing between both syntax. I would choose export = before export default, simply because of no API breakage.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 11, 2016

No it isn't really practical at all, because it isn't "magic" that produces the default export. It is added as a property on the export of the module. What is provided above is trying to export something as my-module.default and then overwriting it with a function named test and TypeScript is preventing you from describing something that contradicts itself.

I think what you might want to consider the solution to #5285 (--allowSyntheticDefaultImports), which allows you to import a module as if it were the shape you denoted.

@tinganho
Copy link
Contributor Author

I think what you might want to consider the solution to #5285 (--allowSyntheticDefaultImports), which allows you to import a module as if it were the shape you denoted.

@kitsonk thanks that was what I wanted.

@tinganho
Copy link
Contributor Author

tinganho commented Sep 11, 2016

There could be a problem. I'm currently doing the magic on the module itself and not relying on any module loader doing the magic for me. There could possibly be people that are not using --allowSyntheticDefaultImports so they cannot use one of my modules, unless they want to use the old import syntax. Telling all users that they need to add a flag just to use a module is little bit harsh also.

FYI. This is the magic I do:

declare var module: any;
module.exports = myModule;
module.exports.default = myModule;

@tinganho tinganho reopened this Sep 11, 2016
@kitsonk
Copy link
Contributor

kitsonk commented Sep 11, 2016

declare module 'my-module' {
    const test: () => string & { default: () => string };
    export = test;
}

@tinganho
Copy link
Contributor Author

Very neat trick!

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Sep 12, 2016
@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.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants