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.

Type annotation for all exports in the module

See original GitHub issue

Search Terms

Export, typed exports, modules, Next.js, Redwood.js

Suggestion

Allow a new syntax From issue #420 that will allow typing all of exports in a module.

export implements {
  default: string
  foo: (bar: boolean) => number
}

export default "Hello world!";
export function foo(bar) { return 1 }

Use Cases and Examples

Increasingly frameworks are using named module exports as a way of organizing their code. For instance Next.js is using named exports to specify things like exported component and functions for fetching data:

import { PageModule } from 'next'

type Post = { title: string };
export implements PageModule<{ posts: Posts[] }>;

function Blog({ posts }) {
  return (
    <ul>
      {posts.map(post => (
        <li>{post.title}</li>
      ))}
    </ul>
  )
}

export async function getStaticProps() {
  return { props: { posts: [] } }
}

export default Blog

Frameworks relying on named export

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Related issues

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:481
  • Comments:40 (5 by maintainers)

github_iconTop GitHub Comments

29reactions
kentcdoddscommented, Nov 29, 2021

I didn’t notice this issue when I opened: https://github.com/microsoft/TypeScript/issues/46942

There are a few more details for use cases and an alternative syntax, but I don’t really care too much about the specific syntax. I just want the feature. 😃 You’ve got a 👍 from me!

9reactions
balazsorban44commented, May 12, 2020

You can add Remix to this category

https://blog.remix.run/p/remix-preview

It will export a load function similar to Next.js’s getServerSideProps

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flow Annotate-Exports
This command uses types that Flow infers, to fill in positions that would otherwise raise signature-verification failures. It will include the necessary type...
Read more >
Documentation - Modules - TypeScript
Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword.
Read more >
Export all types (and just types) from typescript module
This is an option. But for our use case I don't want users of our library to use these classes, only to use...
Read more >
Asking for required annotations - Medium
In order to infer types in input positions which are exported from a module, Flow would need to analyze files together with all...
Read more >
PEP 484 – Type Hints - Python Enhancement Proposals
Any function without annotations should be treated as having the most general type possible, or ignored, by any type checker. Functions with the...
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 Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found