Title: New Syntax: module implements Type · Issue #46942 · microsoft/TypeScript · GitHub
Open Graph Title: New Syntax: module implements Type · Issue #46942 · microsoft/TypeScript
X Title: New Syntax: module implements Type · Issue #46942 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms module interface type module implements module type ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the ...
Open Graph Description: Suggestion 🔍 Search Terms module interface type module implements module type ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/J...
X Description: Suggestion 🔍 Search Terms module interface type module implements module type ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScri...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/46942
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"New Syntax: module implements Type","articleBody":"# Suggestion\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\n- module interface type\r\n- module implements\r\n- module type\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nI want to add a new syntax for setting export types for a module:\r\n\r\n```ts\r\nimport type {MyType} from './types'\r\n\r\nmodule implements MyType\r\n```\r\n\r\nShorthand syntax:\r\n\r\n```ts\r\nmodule implements {MyType} from './types'\r\n```\r\n\r\nDefault type export shorthand syntax:\r\n\r\n```ts\r\nmodule implements {default} from './types'\r\n```\r\n\r\nNote: I'm much more interested in the feature than the syntax.\r\n\r\n## 📃 Motivating Example\r\n\r\nI want to be able to do this:\r\n\r\n```ts\r\n// types.ts\r\nexport type Math = {\r\n add: (a: number, b: number) => number\r\n subtract: (a: number, b: number) => number\r\n multiply?: (a: number, b: number) => number\r\n pi: number\r\n}\r\n\r\n// my-math-module.ts\r\nimport type {Math} from './types'\r\n\r\nmodule implements Math // \u003c-- this is the feature I want\r\n\r\nexport function add(a, b) { // \u003c-- the types are set by and checked against the module implements syntax\r\n return a + b\r\n}\r\n\r\nexport function subtract(a, b) { // \u003c-- the types are set by and checked against the module implements syntax\r\n return a - b\r\n}\r\n\r\n// extra exports are fine, they just don't inherit the type\r\nexport function divide(a: number, b: number): number {\r\n return a / b\r\n}\r\n\r\n// optional types do not have to be exported, so no multiply here\r\n\r\n// not just functions!\r\nexport const pi = 3.14\r\n```\r\n\r\nSee, you can have a conventional module format and get automatic typings for everything the module is supposed to export.\r\n\r\n## 💻 Use Cases\r\n\r\nSome libraries have conventions for modules. I thinking specifically of [Remix](https://remix.run/docs/en/v1/api/conventions#route-module-api), but other frameworks have similar conventions (like [Next.js](https://nextjs.org/docs/basic-features/pages), [SvelteKit](https://kit.svelte.dev/docs), and [Storybook](https://storybook.js.org/docs/react/get-started/whats-a-story)). For example, in Remix a route module should export the following interface:\r\n\r\n```ts\r\ninterface EntryRouteModule {\r\n CatchBoundary?: CatchBoundaryComponent;\r\n ErrorBoundary?: ErrorBoundaryComponent;\r\n default?: RouteComponent;\r\n handle?: RouteHandle;\r\n links?: LinksFunction;\r\n meta?: MetaFunction | HtmlMetaDescriptor;\r\n action?: ActionFunction;\r\n headers?: HeadersFunction | { [name: string]: string };\r\n loader?: LoaderFunction;\r\n}\r\n```\r\n\r\nThe issue here is that as a user of the convention, I have to type everything I export. This comes with two problems:\r\n\r\n1. If I forget, I don't have anything running at compile time to tell me I forgot.\r\n2. It's extra typing (both figuratively and literally)\r\n3. If what I'm exporting is a function, I can't easily use a function declaration which I prefer.\r\n\r\nSo, for example, I currently have to do this:\r\n\r\n```ts\r\nimport {type LoaderFunction} from 'remix'\r\n\r\nexport const loader: LoaderFunction = async ({request}) => {\r\n return {url: request.url}\r\n}\r\n```\r\n\r\nWhat I want to be able to do is this:\r\n\r\n```ts\r\nmodule implements {RouteModule} from 'remix'\r\n\r\nexport async function loader({request}) {\r\n return {url: request.url}\r\n}\r\n```\r\n\r\nPlease let me know if you need any other details. Thanks for taking the time to read and consider!","author":{"url":"https://github.com/kentcdodds","@type":"Person","name":"kentcdodds"},"datePublished":"2021-11-29T05:07:55Z","url":"https://github.com/microsoft/TypeScript/issues/46942"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:66ba93b6-2a5c-b112-338e-8c62dd5bd4ff |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C00A:2C4BED:237E643:2D142E6:6B01821D |
| html-safe-nonce | 80667309242905545264939abdd3fe5ed42434da9882d054b9de1afdb8588199 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDBBOjJDNEJFRDoyMzdFNjQzOjJEMTQyRTY6NkIwMTgyMUQiLCJ2aXNpdG9yX2lkIjoiNDM0NzUzOTkyMjI5MTM2MDI4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8b63111758c08a48d7c2ff5b21358ca66e27f2f8decbd29090fbf245710b99bb |
| hovercard-subject-tag | issue:1065616032 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/46942/issue_layout |
| twitter:image | https://opengraph.githubassets.com/022d090aa6fe27c3d9edc318f13c118042b93655d126408c2ffc180f7e9c7aa0/microsoft/TypeScript/issues/46942 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/022d090aa6fe27c3d9edc318f13c118042b93655d126408c2ffc180f7e9c7aa0/microsoft/TypeScript/issues/46942 |
| og:image:alt | Suggestion 🔍 Search Terms module interface type module implements module type ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/J... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kentcdodds |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width