microsoft / microsoft/TypeScript

Add a modifier for "closed" blocks and functions

Open
#37,028 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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
  })
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or compiler entry points are named. Start by reviewing the proposed closed and closed inline syntax and the serialization and webpack worker use cases. Done means captured outer values are rejected, explicit imports work, and the requested inline behavior is defined and implemented.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, webpack
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.