TypeScript 4.0 variadic tuples: Preserving args through a wrapper
A retrospective on TypeScript 4.0 tuple spreads, consistent wrapper signatures and compile-time regression tests.
What an array type forgets
A telemetry wrapper may accept calls whose first argument is text, second is a number and third is optional. A single array element type does not express that positional contract. A tuple can. TypeScript 4.0 introduced generic spreads within tuple type syntax, reducing the need to write an overload for every supported argument length. This is a compile-time improvement, so runtime call behavior still needs its own checks.
Preserve the source signature
When a wrapper measures time and forwards a call, represent the source parameters as a generic tuple and tie the return type to the source function. Avoid widening the inputs to an unrelated array type. Aim for comparable diagnostics whether a caller uses the wrapper or the original function. If you wrap a method, check the runtime receiver independently. Carrying the tuple shape does not automatically preserve the value of this.
function wrap<Args extends unknown[], Result>(fn: (...args: Args) => Result) {
return (...args: Args): Result => fn(...args);
}Separate the prefix from remaining arguments
Some wrappers partially apply configuration before accepting per-call input. A client can capture an endpoint and policy first, leaving a record ID for the call site. Typing the prefix and remainder as separate tuples helps preserve their order and expected lengths. Values declared as arbitrary arrays may lose positional precision. Inspect inference before supplying casts that could conceal wrong orders or missing values.
Test the type and the value
Use a positive compile-time test for the expected invocation and a negative test placing two values in the wrong order. Pin the compiler version when reproducing the behavior. Add a runtime test that the wrapper actually passes the same arguments in order and returns the expected result. Error propagation and untrusted JavaScript input should still be checked where type information alone cannot enforce the contract.
Review overloads before a migration
Some overloads represent different behavior and return types as well as different parameter counts. Start with two representative callers and inspect completion, diagnostics and emitted behavior. If the library publishes declaration files, check whether supported older compilers can parse them. A smaller declaration is useful when its behavior remains clear to both library authors and callers.
Comments
No comments yet. Be the first to contribute.
Reading “TypeScript 4.0 variadic tuples: Preserving args through a wrapper” raised this question for me: migration and rollback plans should be explained.
Editor note: Forward-looking claims in an announcement are not the same as established performance results. Further evidence may change the assessment.
It is important to distinguish what the source verifies from projections about the future.
The next version's measurements should be compared with the initial announcement.
✎ Write a comment