iterall
iterall copied to clipboard
$$asyncIterator doesn't seem to work with TypeScript
When I define a function with:
public [$$asyncIterator]() {
...
}
instead of
public [Symbol.asyncIterator]() {
...
}
TypeScript's type checking doesn't seem to understand that $$asyncIterator is equivalent to Symbol.asyncIterator. I get the following issue when I try to use a for await... of loop:
src/test/pulsar-client-test.ts:167:37 - error TS2504: Type 'PubSubAsyncIterator<{}>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.
167 for await (const message of ai) {
~~
my tsconfig.json is:
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"noImplicitAny": false,
"rootDir": "./src",
"outDir": "./dist",
"pretty": true,
"removeComments": true,
"declaration": true,
"lib": ["esnext", "dom"]
},
"exclude": [
"node_modules",
"dist",
]
}
Unfortunately this won't be possible until https://github.com/Microsoft/TypeScript/issues/27525 is solved. As mentioned there, we can reference custom symbols with typeof, but that doesn't work with well-known symbols such as Symbol.asyncIterator