Magus
Magus

Reputation: 15104

Add the type of the variable in a for..of loop on a Readable

I have the following loop:

for await (const element of readable) {

readable inherit of the Readable class. But I know that the objects in this Readable will always be of class MyClass.

How can I say to Typescript that element is of MyClass type ? With the current code, Typescript thinks that element is of any type.

Of course I can cast it in a new variable like this :

for await (const e of readable) {
    const element: MyClass = e;

But I wonder if there is a more elegant way.

Upvotes: 1

Views: 577

Answers (1)

Artyom Smirnov
Artyom Smirnov

Reputation: 366

Cast readable to AsyncIterable<MyClass>

for await (const e of (readable as AsyncIterable<MyClass>))

Upvotes: 2

Related Questions