-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Proposal - Module Interface #58029
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
Well, I guess this syntax is a bit confusing as it's neither an internal module nor ESM... module implements UserModuleInterface {
export async function getUser(id) {}
export async function createUser() {}
} The rest appears to be ESM. |
@snarbies I just need to say that that specific module is implementing an interface. I don't know if the suggested syntax is good or not, but it was the only thing that came to mind at the moment =) And yes: the proposal is to maintain the ESM syntax, and just add |
It does feel like #420 roughly captures what's being described here, so I'm going to mark this as a duplicate. |
@DanielRosenwasser should I comment this proposal in #420? |
Yeah, I'd try to keep discussion there going forward. |
π Search Terms
module interface, interface for modules, module implements interface
β Viability Checklist
β Suggestion
Module Interface Proposal
I found some old suggestions to this feature, but I think none is clean like this I'm proposing.
The main idea is give the ability to create an interface to a module. Something like was proposed in #420, but different: my proposal is adding a way to define a module interface, and a way to use it, adding the
module interface
andmodule implements
keywords.π Motivating Example
Explaining the example
First thing we have to do is creating a
module interface
. It can be exported from a specific file astype
or used in the same file that functions will be implemented.Everything inside a
module interface
must be implemented whenmodule implements
is used.Then you can import (as a
type
) thismodule interface
into another file and usemodule implements
to implement the interface. This can be a linemodule implements MyInterface
or something with a body, like:We can also implement more than one interface, like in classes:
Rules
The module that is using
module implements
must implement all functions defined inmodule interface
. Otherwhise, TypeScript should throw an error.Shouldn't be a problem to implement other functions that are not defined in a
module interface
, as long as we implement at least every function defined inmodule interface
.We can possibly make some functions optional (not required to this suggestion):
π» Use Cases
In this proposal, we do not rely on runtime features. All types (
module interface
andmodule implements
) can be safely removed in build time.The functions defined inside a module that extends a
module interface
can be auto-inferred.And with this implementation, we have tree-shaking, because we can import only the functions we'll use.
To do something similar today, we have to create a
class
or an object and export the whole object, even if we want to use only one function.The text was updated successfully, but these errors were encountered: