Avoid nesting IIFE for nested namespaces #31072
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
iife namespace nest
Suggestion
Produce a single IIFE for a nested namespace, where applicable.
The point is to reduce onion-like layering in scenarios where it is both trivial and safe to do. There would still be at least 1 level of IIFE wrapping. Every aspect of runtime/semantic/scoping behaviours is preserved as in the current emit.
Example
TypeScript Playground
Use Cases
Nested IIFE can affect performance, epecially in older/constrained environments. Of course, in general case they cannot be avoided, as they create lexical scopes and may have code living in all those nesting levels.
But there are several easy-to-detect scenarios where avoiding IIFE infestation is cheap and easy:
namespace A.B.C
with single statement of dot-separated nest chainnamespace A { namespace B {} }
with namespace having a single AST node child of another namespaceexport
modifierNote that this feature only affects JS emits, but not parsing neither declaration handling.
Also, this may feel related to #447 Partial modules and output optimization, but the suggestion there is much more broad and heavy to implementation.
Here I do not suggest merging of scopes, just eliminating an easy-to-detect subset of empty scopes that have no code in them.
Non-targeted cases
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: