Type annotation for all exports in the module
See original GitHub issueSearch 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
- Next.js
- Redwood.js
- Remix.js
- SevlteKit
- StroryBook
- CloudFlare Workers
-
please suggest more
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:
- Created 3 years ago
- Reactions:481
- Comments:40 (5 by maintainers)
Top 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 >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
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
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