Reputation: 15104
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
Reputation: 366
Cast readable to AsyncIterable<MyClass>
for await (const e of (readable as AsyncIterable<MyClass>))
Upvotes: 2