Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a modifier for "closed" blocks and functions #37028

Open
FilipeBeck opened this issue Feb 25, 2020 · 2 comments
Open

Add a modifier for "closed" blocks and functions #37028

FilipeBeck opened this issue Feb 25, 2020 · 2 comments
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

Comments

@FilipeBeck
Copy link

FilipeBeck commented Feb 25, 2020

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 extra inline keyword in the closed 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 like awaiter 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 a closed inline block, these problems are resolved without needs to use any extra plugin or loader.

Examples

const path = require('path')

closed {
  const pathSeparator = path.sep // Compiler error - `path` is not defined

  import('path').then(module => { // The right way
    // ...
  })
}

closed inline {
  const pathSeparator = path.sep // Compiler error - `path` is not defined

  import('path').then(module => { // The right way
    // The path module will be inlined within the `closed inline` block
  })
}

closed function foo() {
  const pathSeparator = path.sep // Compiler error - `path` is not defined

  import('path').then(module => { // The right way
    // ...
  })
}

closed inline foo() {
  const pathSeparator = path.sep // Compiler error - `path` is not defined

  import('path').then(module => { // The right way
    // The path module will be inlined within the `closed inline` function
  })
}
@RyanCavanaugh RyanCavanaugh added 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 labels Feb 25, 2020
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 26, 2020

So is the intent just to ensure that no outer environments/variables are captured by the function?

@FilipeBeck
Copy link
Author

Yes, that's it. Also, an compiler error should occur if a non-closed function is provided where a closed function is expected

@FilipeBeck FilipeBeck changed the title Add a modifier for "closed" functions Add a modifier for "closed" blocks and functions Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants