You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// my-person.tsimport{Person}from"./interface";// this is new and does the following// require all exports to be one of the member properties of Person// enables autocomplete on exported token names// types exported tokens according to matching member names in PersontypeexportwithPerson;// this named export is required and must be a stringexportconstname="Foo";// this named export is optional, but if defined must be a numberexportconstage=42;// not sure if other exports should be allowed
// other.tsimport*aspersonfrom"./my-person";// person has type Person as defined in interface.ts
π Motivating Example
Next.js has many files which it looks for. Next expects these files to export members conforming to its own interface. Currently it's up to developer to type each individual member separately. These types need to be separately imported from Next as well. Documentation (if it exists) needs to be consulted for each expected member export.
type export with solves or improves many of these issues. type export with defines what tokens should be exported, so auto-complete can be used to fill in names after typing export const .... Furthermore it defines types for each of these tokens, so individual tokens don't need to be independently typed.
Here's an example of what is currently needed to properly type a layout.
import{Metadata}from"next";import{ReactNode}from"react";exportconstgenerateMetadata=async()=>{const{ t }=awaitloadPageTranslations("layout-root");return{title: t("title"),description: t("description")}satisfiesMetadataasMetadata;};exportdefaultasyncfunctionRootLayout({
children
}: {children: ReactNode;// other props must be individually typed}){// nothing verifies the component return typereturn(<html><body><main>{children}</main></body></html>);}
Here's how this file could be typed with this proposal.
import{LayoutFile}from"next";typeexportwithLayoutFile;// generateMetadata is optional// if defined it must be an async function with a return value of a specific shape// `generateMetadata` is also mutually exclusive with `metadata`, so error if both are definedexportconstgenerateMetadata=async()=>{const{ t }=awaitloadPageTranslations("layout-root");// return object key names are autocompleted and values are type-checkedreturn{title: t("title"),description: t("description")};};// default export is required to be defined by LayoutFile and must satisfy React's component type with proper typings for expected propsexportdefaultasyncfunctionRootLayout({ children }){return(<html><body><main>{children}</main></body></html>);}
π» Use Cases
What do you want to use this for? I want strict type-checking with as little friction as possible. This would be especially helpful for frameworks that expect certain files to define certain exports.
What shortcomings exist with current approaches? Assuming types are provided, each individual export in a file needs to be typed separately. There is no mechanism to verify exports conform to a particular interface.
What workarounds are you using in the meantime? Importing multiple types and using them ad-hoc to check exported members, as shown above.
The text was updated successfully, but these errors were encountered:
π Search Terms
β Viability Checklist
β Suggestion
Allow typing all exported tokens as if they were members of some defined interface or type.
π Motivating Example
Next.js has many files which it looks for. Next expects these files to export members conforming to its own interface. Currently it's up to developer to type each individual member separately. These types need to be separately imported from Next as well. Documentation (if it exists) needs to be consulted for each expected member export.
type export with
solves or improves many of these issues.type export with
defines what tokens should be exported, so auto-complete can be used to fill in names after typingexport const ...
. Furthermore it defines types for each of these tokens, so individual tokens don't need to be independently typed.Here's an example of what is currently needed to properly type a layout.
Here's how this file could be typed with this proposal.
π» Use Cases
The text was updated successfully, but these errors were encountered: