Allow a module to implement an interface
See original GitHub issueIt would be useful when a module can implement an interface using the implements
keyword. Syntax: module MyModule implements MyInterface { ... }
.
Example:
interface Showable {
show(): void;
}
function addShowable(showable: Showable) {
}
// This works:
module Login {
export function show() {
document.getElementById('login').style.display = 'block';
}
}
addShowable(Login);
// This doesn't work (yet?)
module Menu implements Showable {
export function show() {
document.getElementById('menu').style.display = 'block';
}
}
addShowable(Menu);
Issue Analytics
- State:
- Created 9 years ago
- Reactions:85
- Comments:58 (15 by maintainers)
Top Results From Across the Web
Why can't TypeScript modules implement or adhere to an ...
When I was registering them, I was passing the actual module into a method and I just made that method only accept an...
Read more >Implementing an Interface in Python - Real Python
In this tutorial, you'll explore how to use a Python interface. You'll come to understand why interfaces are so useful and learn how...
Read more >Handbook - Interfaces - TypeScript
One of TypeScript's core principles is that type checking focuses on the shape that values have. This is sometimes called “duck typing” or...
Read more >Implements statement (VBA) - Microsoft Learn
The example also shows how use of an interface allows abstraction: a strongly-type variable can be declared by using the interface type.
Read more >C++20: Module Interface Unit and Module Implementation Unit
Thanks to the module interface unit and the module implementation unit, you can separate the interface from the implementation when defining ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
One use case would be for frameworks and tools that scan a directory for modules on application startup, expecting those modules all to export a certain shape.
For example, Next.js scans
./pages/**/*.{ts,tsx}
for your page modules, generating routes based on your filenames. It’s up to you to ensure each module exports the right things (aNextPage
as the default export, and an optionalPageConfig
export namedconfig
):It would be nice if you could instead declare the export shape of the whole module in one line near the top, like
implements NextPageModule<Props>
.Another thought: it would be interesting if there was some way to specify in a TypeScript config that all files matching a certain pattern (like
./pages/**/*.{ts,tsx}
) must implement a certain export shape, so a module could have its exports type-checked purely because it’s located within thepages
directory for example. But I’m not sure if there’s any precedent for this approach, and it might get confusing.Another use case for this:
Universal modules for React Native & React Native Web, where the bundler for each platform picks the right file for a certain ambiguous import like this:
File list:
Currently, there’s no convenient way to enforce that all these files must have the same exports. The absence of module-level type assertion could realistically lead to runtime crashes.