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
A way to declare the export signature of a whole file.
Consider, for instance, NextJS pages. Every page must default-export a React Component that satisfies a certain type and may export additional methods, such as getStaticProps. Currently, every export in every file must be explicitly annotated, which is repetitive and error- and inconsistency-prone.
Instead, (on a per-file-basis?; with glob src/pages/**/*?) exports for the entire file can be declared at once, which comes with the added benefit of automatic type inference for parameters, return types etc. for exported stuff.
π Motivating Example
When writing pages for NextJS, annotate page files with
Many systems (both backend and frontend) use a modularized approach for declaring essentially anything. Consider a CMS whose individual components are found under something like:
// src/components/index.tsimport*asBoxfrom"./Box";import*as ... from"./...";exportinterfaceCMSComponent{ID: string;description?: string;render(...): ...;renderEdit(...): ...;}BoxsatisfiesCMSComponent;// ^^^ Property 'renderEdit' is missing in type '{ ID: string; render: (...) => ... }' but required ...
... satisfiesCMSComponent;export{Box,
...,};
The X satisfies CMSComponent is currently the best way to check the exported types. The problem of this is that if a component were, for instance, missing props, the error would be thrown in the wrong place. It would be the file that exported properties incorrectly, not the index file that used a file incorrectly.
With this proposal, an incorrect file would throw an error something like the following:
// src/components/Box.tsx/// <exports satisfy=".#CMSComponent" />// ^^^ This file does not satisfy its export types. Property 'renderEdit' is missing in type '{ ID: string; render: (...) => ... }' but required ...exportconstID= ...;exportconstrender=(...)=>{ ... };
Consider a server-side project, in which such an index file may not even exist, with files being imported dynamically. Here, the user would need to make sure that all files would be set up correctly to not run into runtime errors.
With some way to declare to declare glob-based export patterns, this could be prevented entirely:
Suggestion
π Search Terms
File implements interface, file satisfies, file interface
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
A way to declare the export signature of a whole file.
Consider, for instance, NextJS pages. Every page must default-export a React Component that satisfies a certain type and may export additional methods, such as
getStaticProps
. Currently, every export in every file must be explicitly annotated, which is repetitive and error- and inconsistency-prone.Instead, (on a per-file-basis?; with glob
src/pages/**/*
?) exports for the entire file can be declared at once, which comes with the added benefit of automatic type inference for parameters, return types etc. for exported stuff.π Motivating Example
When writing pages for NextJS, annotate page files with
π» Use Cases
Many systems (both backend and frontend) use a modularized approach for declaring essentially anything. Consider a CMS whose individual components are found under something like:
A component can be described with the following interface:
with the index file re-exporting components with
The
X satisfies CMSComponent
is currently the best way to check the exported types. The problem of this is that if a component were, for instance, missing props, the error would be thrown in the wrong place. It would be the file that exported properties incorrectly, not the index file that used a file incorrectly.With this proposal, an incorrect file would throw an error something like the following:
Consider a server-side project, in which such an index file may not even exist, with files being imported dynamically. Here, the user would need to make sure that all files would be set up correctly to not run into runtime errors.
With some way to declare to declare glob-based export patterns, this could be prevented entirely:
The text was updated successfully, but these errors were encountered: