Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-paging] Update the return type for "next" method in PagedAsyncIterableIterator #10599

Merged

Conversation

HarshaNalluru
Copy link
Member

@HarshaNalluru HarshaNalluru commented Aug 12, 2020

Issue

done is a required property in the IteratorResult<T> if the typescript version is 3.1(for all <3.6 probably).
Whereas done is optional in the return type of the "next()" method in the PagedAsyncIterableIterator.
Compilation with tsc(TS3.1) fails due to this type mismatch when the target version is higher than ES5(example: ES6 or ESNEXT).

src/advanced/listingEntities.ts:40:29 - error TS2322: Type 'PagedAsyncIterableIterator<QueueProperties, EntitiesResponse<QueueProperties>, PageSettings>' is not assignable to type 'AsyncIterable<QueueProperties>'.
  Types of property '[Symbol.asyncIterator]' are incompatible.
    Type '() => PagedAsyncIterableIterator<QueueProperties, EntitiesResponse<QueueProperties>, PageSettings>' is not assignable to type '() => AsyncIterator<QueueProperties>'.
      Type 'PagedAsyncIterableIterator<QueueProperties, EntitiesResponse<QueueProperties>, PageSettings>' is not assignable to type 'AsyncIterator<QueueProperties>'.
        Types of property 'next' are incompatible.
          Type '() => Promise<{ done?: boolean; value: QueueProperties; }>' is not assignable to type '(value?: any) => Promise<IteratorResult<QueueProperties>>'.
            Type 'Promise<{ done?: boolean; value: QueueProperties; }>' is not assignable to type 'Promise<IteratorResult<QueueProperties>>'.
              Type '{ done?: boolean; value: QueueProperties; }' is not assignable to type 'IteratorResult<QueueProperties>'.        
                Property 'done' is optional in type '{ done?: boolean; value: QueueProperties; }' but required in type 'IteratorResult<QueueProperties>'.

 
40   for await (const queue of queues) {
                               ~~~~~~

Fix

Instead of declaring our own return type for the next() method, use IteratorResult<T> to avoid type mismatch.

@ghost ghost added the Azure.Core label Aug 12, 2020
@HarshaNalluru HarshaNalluru marked this pull request as ready for review August 12, 2020 22:20
@xirzec
Copy link
Member

xirzec commented Aug 15, 2020

Update PR title to reflect this is a core-paging change?

@HarshaNalluru HarshaNalluru changed the title [Service Bus] Update the return type for "next" method in PagedAsyncIterableIterator [core-paging] Update the return type for "next" method in PagedAsyncIterableIterator Aug 15, 2020
@HarshaNalluru
Copy link
Member Author

HarshaNalluru commented Aug 15, 2020

More info :

While IteratorResult<T> compiles well with 3.1, 3.6 and 3.9. (value assumes "any" type if TS version>3.5)
IteratorResult<T, T> compiles fine only for TS version > 3.5.

For TS version < 3.6, IteratorResult takes only one argument -> IteratorResult<T>.
For TS version >= 3.6, IteratorResult takes two arguments -> IteratorResult<T, TReturn = any>(the second one is optional).

The only solution is to have different type definitions for lower TS versions.

Created an issue at sandersn/downlevel-dts#40

/**
* @member {Promise} [next] The next method, part of the iteration protocol
*/
next(): Promise<IteratorResult<T>>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added separate .dts file for 3.1 to 3.5.

@jeremymeng @bterlson

Copy link
Member

@jeremymeng jeremymeng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Copy link
Member

@xirzec xirzec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants