#Spreading iterator as function arguments

7 messages · Page 1 of 1 (latest)

novel shoal
#

A weird pattern, but is this possible to make TS aware that spreading Foo will effectively result in [number, number] and thus allowed to be passed as arguments to func?

class Foo {
    *[Symbol.iterator]() {
        yield 1
        yield 2
    }
}

declare function func(num1: number, num2: number): void

func(...new Foo())
pliant arch
#

nope

#

Foo is an Iterable<1 | 2>

#

i don't think there's a way to specify a tuple instead of the item type

pliant arch
#

(@novel shoal -^)

novel shoal
#

Yeah I figured as much.

#

Thanks @pliant arch