Syntax Mechanics
An IIFE consists of two primary syntactic components:- The Grouping Operator
(...): Provides an expression context for thefunctionkeyword and its body, ensuring the compiler parses the entire construct as a function expression rather than a function declaration. - The Invocation Operator
(...): Appended immediately after the grouping operator to execute the evaluated expression synchronously.
Standard Function Expression IIFE
You can explicitly type the parameters and the return type of the anonymous function.Arrow Function IIFE
The arrow function syntax provides a more concise lexical structure. The type annotations follow standard TypeScript arrow function semantics.Asynchronous IIFE
When utilizingasync/await within an IIFE, the function expression must be marked as async, and the return type must be typed as a Promise.
Generic IIFE
TypeScript allows IIFEs to accept generic type parameters. The generic type is declared before the parameter list and instantiated at the invocation operator.Semicolon Prefixing
When an IIFE follows another statement in a file, it is a standard syntactic practice to prefix the grouping operator with a semicolon. This prevents the TypeScript compiler from incorrectly interpreting the IIFE’s grouping operator as a function call on the preceding line’s evaluated result.Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More





