I have a method that should accept either an array of numbers or accept a variable number of number arguments (variadic). In most languages I've used, when you make a method/function variadic, it accepts both, but it seems that in TypeScript, you cannot. When I make this particular function variadic, all of the places where I supply a number[] fail compilation.
Signature for reference (in class ObjectIdentifier):
constructor(... nodes : number[]) {
This fails:
return new ObjectIdentifier(numbers);
where numbers is of type number[].
Use the following syntax:
const func = (...a: number[]) => console.info('a', a)
func(1, 2, 3)
func(...[1, 2, 3])
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