question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow a module to implement an interface

See original GitHub issue

It 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:open
  • Created 9 years ago
  • Reactions:85
  • Comments:58 (15 by maintainers)

github_iconTop GitHub Comments

19reactions
callumlockecommented, Oct 9, 2019

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 (a NextPage as the default export, and an optional PageConfig export named config):

import { NextPage, PageConfig } from 'next'

interface Props { userAgent?: string }

const Home: NextPage<Props> = ({ userAgent }) => (<main>...</main>)

Page.getInitialProps = async ({ req }) => {
  const userAgent = req ? req.headers['user-agent'] : navigator.userAgent
  return { userAgent }
}

export default Page

export const config: PageConfig = {
  api: { bodyParser: false }
}

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 the pages directory for example. But I’m not sure if there’s any precedent for this approach, and it might get confusing.

17reactions
forabicommented, Dec 26, 2020

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:

import {analytics} from './analytics'

File list:

analytics.web.ts
analytics.android.ts
analytics.ios.ts`

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found