iterall icon indicating copy to clipboard operation
iterall copied to clipboard

$$asyncIterator doesn't seem to work with TypeScript

Open grantwwu opened this issue 7 years ago • 1 comments

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",
  ]
}

grantwwu avatar Feb 22 '19 02:02 grantwwu

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

stephentuso avatar Mar 09 '19 23:03 stephentuso