-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow declaring that module is also function / constructor without declaration merging #5867
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
Comments
ES6 modules do not allow for changing the module shape and/or exporting the module object as a function. the only way you would do this in ES6 is by exporting |
I don't propose this syntax to be valid outside of declarations. Also, the facts are:
|
we already have a concept of declaration merging, as well as export =. we can not remove these, or we will be breaking a lot of code out there. what i am saying is we should not add yet another way. |
I think you should reconsider this, as it remains the biggest pain point in writing definition files for node modules. After all, constructors and functions are already allowed in interfaces, and arbitrary restrictions to forbid them on modules are both nonsensical and harmful to usability. As shown above, Writing definition files still feels like navigating a maze of gotchas. This change will greatly contribute towards reducing that problem. |
Also note that I'm not saying to remove Furthermore I definitely think declaration merging should be kept as its useful in many more situations than the
There is no more reason to import redis into a namespace now (which is not allowed by the compiler anyway) and the namespace hack can simply be removed from |
Use case:
express.d.ts
on DefinitelyTyped:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/express/express.d.ts
Here, express exposes a namespace
Express
to let us extendRequest
via declaration merging.Unfortunately those extensions cannot contain types imported from ambient external
"modules"
, which means they're of limited use. You can'timport {RedisClient} from 'redis'
and define that such a client is attached to aRequest
(you're not allowed to import ambient external modules into a namespace declaration)However, since express-the-module is also a function, the only way to declare the module is to use declaration merging then write
export = mergeCombination
at the end. This means that we can't use ambient external module merging eitherSince lots of node modules love this "module is function and also namespace" approach, this isn't an isolated case.
I really think we need syntax that allows us to export functions and class constructors from module declarations together with everything else. Declaration merging with
export = x
syntax is cool, but it really is a hack when its used because of the lack of this feature.Syntax (just to get started, I'm sure someone will have better ideas)
The text was updated successfully, but these errors were encountered: