Skip to content

Fix return type of NodeJS.ReadableStream#read#26373

Closed
rubensworks wants to merge 2 commits intoDefinitelyTyped:masterfrom
rubensworks:fix/node-readable-read
Closed

Fix return type of NodeJS.ReadableStream#read#26373
rubensworks wants to merge 2 commits intoDefinitelyTyped:masterfrom
rubensworks:fix/node-readable-read

Conversation

@rubensworks
Copy link
Copy Markdown
Contributor

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

If changing an existing definition:

@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Jun 9, 2018
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Jun 9, 2018

@rubensworks Thank you for submitting this PR!

🔔 @eps1lon @Archcry @WilcoBakker @inlined @Alorel @KSXGitHub @parambirs @tellnes @octo-sniffle @smac89 @Flarna @mwiktorczyk @wwwy3y3 @DeividasBakanas @kjin @alvis @Hannes-Magnusson-CK @jkomyno @hoo29 @n-e @BrunoScheufler @islishude @r3nya @ajafff @mohsen1 @a-tarasyuk @eyqs @ZaneHannanAU @ThomasdenH @matthieusieben - please review this PR in the next few days. Be sure to explicitly select Approve or Request Changes in the GitHub UI so I know what's going on.

If no reviewer appears after a week, a DefinitelyTyped maintainer will review the PR instead.

@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Jun 9, 2018

@rubensworks The Travis CI build failed! Please review the logs for more information.

Once you've pushed the fixes, the build will automatically re-run. Thanks!

Copy link
Copy Markdown
Contributor

@hoo29 hoo29 left a comment

Choose a reason for hiding this comment

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

https://github.com/Microsoft/dtslint/blob/master/docs/no-any-union.md states this offers no additional type safety so the pull request in unnecessary.

@KSXGitHub
Copy link
Copy Markdown
Contributor

any only occur under object mode

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Jun 9, 2018
@KonanMentor KonanMentor mentioned this pull request Jun 10, 2018
9 tasks
@KSXGitHub
Copy link
Copy Markdown
Contributor

any is not really helpful. If you really want to support object mode, you should create a ObjectModeBuffer and a system of functions around it.

@rubensworks
Copy link
Copy Markdown
Contributor Author

@KSXGitHub How would you suggest defining such a ObjectModeBuffer ? read() can literally return any type, so I am not sure what this ObjectModeBuffer would look like.

@typescript-bot typescript-bot added Revision needed This PR needs code changes before it can be merged. and removed The Travis CI build failed labels Jun 11, 2018
@typescript-bot
Copy link
Copy Markdown
Contributor

typescript-bot commented Jun 11, 2018

@rubensworks Unfortunately, this pull request currently has a merge conflict 😥. Please update your PR branch to be up-to-date with respect to master. Have a nice day!

@KSXGitHub
Copy link
Copy Markdown
Contributor

KSXGitHub commented Jun 11, 2018

@rubensworks By read(), you meant readdable.read()? you can have a ObjectModeReadable#read that returns any and leave Readable#read for string | Buffer. Another alternate solution is to make Stream/Readdable/Writable generic. ObjectMode* types are returning value when one provides object-mode options (such as objectMode: true, readdableObjectMode: true, writableObjectMode: true) to functions that create Streams.

Personally, I find doing this is way too complicated that it isn't worth it. Not to mention this pull request is guaranteed to break existing TypeScript projects which have always assumed Stream contains Buffer. I also find no real use of object mode anyway.

@rubensworks
Copy link
Copy Markdown
Contributor Author

Another alternate solution is to make Stream/Readdable/Writable generic.
...
Personally, I find doing this is way too complicated that it isn't worth it.

Agreed, this change would be pretty invasive. I'd want second/third/... opinion on this before I implement this.

I also find no real use of object mode anyway.

I'm pretty sure lots of people do have a use for object mode ;-) (This W3C community group is just one example: http://rdf.js.org/#stream-interface)

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Jun 11, 2018
@typescript-bot
Copy link
Copy Markdown
Contributor

🔔 @hoo29 - Thanks for your review of this PR! Can you please look at the new code and update your review status if appropriate?

@typescript-bot typescript-bot added the Revision needed This PR needs code changes before it can be merged. label Jun 11, 2018
@hoo29
Copy link
Copy Markdown
Contributor

hoo29 commented Jun 11, 2018

Losing type safety for the non-object mode streams does not seem optimal. In lieu of doing a substantial pull request to create an ObjectModeReadable interface, would it not be better to simply cast the stream to any in your code?

@rubensworks
Copy link
Copy Markdown
Contributor Author

In lieu of doing a substantial pull request to create an ObjectModeReadable interface, would it not be better to simply cast the stream to any in your code?

Surely, I'm not the only one with this problem. So to let everyone experiencing this cast away the problem seems to go in against the ideas of TypeScript. So I don't consider this a good alternative.

As is stands now, the typings are simply wrong, which needs fixing. This PR does exactly that.
An ObjectModeReadable is indeed a more generic fix, but requires significant changes.

Perhaps one of the core maintainers of DT can chime in and share their thoughts?

@Flarna
Copy link
Copy Markdown
Contributor

Flarna commented Jun 11, 2018

Perhaps one of the core maintainers of DT can chime in and share their thoughts?

I'm not a core maintainer - not sure if there are ones but I can share my thoughts.

I think there is already a #26410 to address this by converting stream into generics. Quite a big PR as it requires to raise minimum typescript version to 2.4 so not sure if this will be merged soon/at all.

To my understanding this would correct the issue mentioned here plus add the possibility to use more concrete typing for e.g HTTP/TCP streams which never use object mode.

To me this looks like a good starting point.

@hoo29
Copy link
Copy Markdown
Contributor

hoo29 commented Jun 11, 2018

Fair enough - I am convinced!

@typescript-bot typescript-bot added Owner Approved A listed owner of this package signed off on the pull request. Merge:Express labels Jun 11, 2018
@typescript-bot
Copy link
Copy Markdown
Contributor

A definition owner has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped!

@KSXGitHub
Copy link
Copy Markdown
Contributor

We should also watch out for breaking changes. It is guaranteed to happen.

@minestarks
Copy link
Copy Markdown
Contributor

I'm holding off on merging this until a call is made on #26410

@typescript-bot typescript-bot added Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. and removed Merge:Express labels Jun 13, 2018
@typescript-bot typescript-bot added Popular package This PR affects a popular package (as counted by NPM download counts). and removed Popular package This PR affects a popular package (as counted by NPM download counts). labels Jun 27, 2018
@ZaneHannanAU
Copy link
Copy Markdown
Contributor

Yeah. Presuming certain ones is awkward.

If we say that streams in normal mode return buffers, we should extend it so it has BufferModeStream, StringModeStream and ObjectModeStream<T>.
Any type is too varied.

@ZaneHannanAU
Copy link
Copy Markdown
Contributor

But that'd just cause concurrency hell…

@typescript-bot typescript-bot added Other Approved This PR was reviewed and signed-off by a community member. Unowned This PR touches a package that doesn't have any listed owners. Owner Approved A listed owner of this package signed off on the pull request. and removed Owner Approved A listed owner of this package signed off on the pull request. Other Approved This PR was reviewed and signed-off by a community member. Unowned This PR touches a package that doesn't have any listed owners. labels Aug 11, 2018
@typescript-bot typescript-bot added Author Approved and removed Revision needed This PR needs code changes before it can be merged. labels Aug 21, 2018
@ghost
Copy link
Copy Markdown

ghost commented Oct 9, 2018

We'll be implementing typesVersions (microsoft/TypeScript/pull/26568) soon on DefinitelyTyped which should allow us to make non-backwards-compatible changes to packages like node. At that point something more type-safe like #26410 would be preferable to just using any.

@ghost ghost closed this Oct 9, 2018
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. Owner Approved A listed owner of this package signed off on the pull request. Popular package This PR affects a popular package (as counted by NPM download counts).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants