Variadic Tuples
Variadic Tuples gives tuples the ability to handle the rest operator (...)
to pass types through type checker in a way that works like generics.
This is quite an advanced topic, so if you get lost do not worry too much.
It builds on example:generic-functions and example:tuples
To start off, here is a variadic tuple which will always prefix another
tuple with a number:
// ^ Generic used to constrain the T
^ ... used to indicate where to merge
This can then be used for composition:
type AddMax
// This mechanism can be combined with multiple input params. For example, this
function merges two arrays but uses '\0' as a sigil to indicate where the arrays
start and stop.
type MaxMin = AddMax<[min: number]>
type MaxMinDiameter = AddMax<[min: number, diameter: number]>
// The same can be used after the tuple:
type SuffixDIContext
// These tools make it possible to correctly type a function like curry which
is a well used concept in functional programming:
function joinWithNullTerminators
// There are three generic arguments:
- T: The params which are array of inputs to the curry function
- U: The parameters which _aren't_ passed into to curry function, and need applying to the return func
- R: the return type of the passed in function
function curry
// You can find a more in-depth explanation, with more code samples in
https://github.com/microsoft/TypeScript/pull/39094
const sum = (left: number, right: number,) => left + right
const a = curry(sum, 1, 2)
const b = curry(sum, 1)(2)
const c = curry(sum)(1, 2)