Add a modifier for "closed" blocks and functions #37028
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Search Terms
puppeteer, worker, webpack
Suggestion
Add a
closed
clause to create blocks/functions that cannot capture any value outside the scope of the block. Any dependencies must be imported explicitly within the block. These dependencies can be inlined to form a self-contained block (using an extrainline
keyword in theclosed
statement or through a boolean field in the compiler options). The resulting inline block can be optimized and unused code can be removed.Use Cases
Serialize functions
Tools like puppeteer provide functions to perform a callback in the browser context. These callbacks are serialized. This can cause problems with functions in TypeScript, since the language uses
tslib
and errors likeawaiter is not defined
can occur. Also, the compiler will not report an error when any value is captured within the callback, but these values will not be available within the browser context.Workers in webpack
There are some problems when using workers with webpack. The emitted code for the
import|require
statements contains functions that will not be available in the worker scope. With aclosed inline block
, these problems are resolved without needs to use any extra plugin or loader.Examples
The text was updated successfully, but these errors were encountered: