Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In TypeScript, a required parameter is a function parameter that mandates an argument to be passed during invocation. By default, every parameter defined in a TypeScript function signature is required. The compiler enforces strict arity, ensuring the number of provided arguments exactly matches the number of required parameters defined in the signature.
function initializeNode(id: string, retries: number): void {
    // Implementation
}

initializeNode("node_01", 3); // Valid
initializeNode("node_01");    // Error: Expected 2 arguments, but got 1.

Structural and Compiler Rules

1. Arity and Type Enforcement When a function is invoked, the compiler checks both the arity (argument count) and the type compatibility. Omitting an argument for a required parameter, or providing an argument of the wrong type, results in a compile-time error (ts(2554) or ts(2345)). 2. Positional Constraints In a function signature, required parameters must strictly precede any optional parameters (denoted by ?) or rest parameters (denoted by ...). A required parameter cannot follow an optional parameter because it creates ambiguity during positional argument resolution.
// INVALID: A required parameter cannot follow an optional parameter.
function configureInvalid(timeout?: number, mode: string): void {} 
// Error: A required parameter cannot follow an optional parameter.

// VALID: Required parameters are declared first.
function configureValid(mode: string, timeout?: number): void {}
3. Interaction with strictNullChecks A required parameter dictates that an argument must be provided, but it does not inherently dictate that the value cannot be undefined. If a parameter must be provided but is allowed to be undefined, it must be typed with a union type. Even if the type accepts undefined, the parameter remains structurally required; the invocation cannot be empty.
function setThreshold(value: number | undefined): void {
    // Implementation
}

setThreshold(10);        // Valid
setThreshold(undefined); // Valid: Argument is explicitly provided and matches type.
setThreshold();          // Error: Expected 1 arguments, but got 0.
4. Default Parameters and Required Arity While assigning a default value (e.g., param: type = value) typically makes a parameter optional, this behavior changes based on positional context. If a default-initialized parameter precedes a required parameter, it is not treated as optional for the purpose of function application. The caller is strictly required to pass an argument (typically undefined to trigger the default value) for that position to satisfy the arity of the subsequent required parameter.
function setup(mode = "auto", timeout: number): void {
    // Implementation
}

setup(undefined, 5000); // Valid: 'mode' defaults to "auto", 'timeout' is 5000.
setup(5000);            // Error: Expected 2 arguments, but got 1.
Master TypeScript with Deep Grasping Methodology!Learn More