I have a function, which I want to ensure takes a string, followed by a number. And optionally, more string number pairs. So like a tuple, but "infinite" times:
const fn = (...args: [string, number] |
[string, number, string, number] |
[string, number, string, number, string, number] |
[string, number, string, number, string, number, string, number] | ...etc etc) => {}
and so on
An alternative psuedo type construction:
type Pair = [string, number, ...Pair];
Is this possible with TypeScript?
EDIT This looks like it should work, but doesn't. TypeScript does not seem to infer the order of the pairs correctly.
type Pair = [string, number, ...Pair[]];
I believe so. You would define the type as
type RecurringTuple = [string, number, RecurringTuple[]];
const fn = (...args: RecurringTuple) => {}
Haven't tested, but found a similar case in this link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With