fix: mistyping of stream by monotykamary · Pull Request #4400 · knex/knex · GitHub
Skip to content

fix: mistyping of stream#4400

Merged
kibertoad merged 3 commits into
knex:masterfrom
monotykamary:fix-mistype-stream
Mar 26, 2021
Merged

fix: mistyping of stream#4400
kibertoad merged 3 commits into
knex:masterfrom
monotykamary:fix-mistype-stream

Conversation

@monotykamary

@monotykamary monotykamary commented Mar 26, 2021

Copy link
Copy Markdown
Contributor

Reverts a breaking type change to stream from #4377. The stream method returns an
instance of stream.Passthrough or allows a callback of an instance stream.Passthrough that returns a Promise as per documentation. This is not interchangeable with AsyncIterable as Readable already implements AsyncIterableIterator.

Adds upon #4377 by intersecting the stream.PassThrough class to maintain the stream type.

@kibertoad

Copy link
Copy Markdown
Collaborator

@kibertoad

Copy link
Copy Markdown
Collaborator

@monotykamary Is it possible to adjust type instead of removing it?

@nguymin4

nguymin4 commented Mar 26, 2021

Copy link
Copy Markdown
Contributor

@kibertoad without AsyncIterable the user type in the code below will be any this is bad practice in my opinion

  const stream = knexInstance<User>('users').select('*').stream();
  for await (const user of stream) {
    // user type is any
  }

@monotykamary

Copy link
Copy Markdown
Contributor Author

@nguymin4 Breaking the type is also bad practice, especially since the stream returns a Duplex and there may be cases where a user needs to read and abort the stream; although I haven't considered that the original pull request was to cover instance types over iterables.

@monotykamary Is it possible to adjust type instead of removing it?

In that case, it might just make sense by intersecting both types:

    stream(): stream.PassThrough & AsyncIterable<ArrayMember<T>>;

@kibertoad

Copy link
Copy Markdown
Collaborator

@monotykamary Could you adjust the PR to do the intersection?

@nguymin4

nguymin4 commented Mar 26, 2021

Copy link
Copy Markdown
Contributor

The intersection will not work. It doesn't resolve the type of user correctly and return any. But yeah if people are using loosely typing then we shouldn't make this change.
From API level point of view, I think we are trying to do too much at the same time with stream() which works in Javascript but not nicely in Typescript. In a sense I feel like the pipe method should be the only one return stream.Passthrough. Or alternative solution I'm thinking about is we can have asIterable() method but maybe too much work and not worth the effort.

@monotykamary

Copy link
Copy Markdown
Contributor Author

The intersection will not work.

It seems to work fine on my end:
image

Does the repository have a specific typescript version target that this doesn't work on?

@nguymin4

nguymin4 commented Mar 26, 2021

Copy link
Copy Markdown
Contributor

Doesn't seems to work for me unfortunately 🤔
Screenshot from 2021-03-26 19-29-24

Edit: Ah damn my bad. It actually works.

@nguymin4

nguymin4 commented Mar 26, 2021

Copy link
Copy Markdown
Contributor

Ok seems to work now. So we can merge those two stream methods into one.
stream(options?: Readonly<{ [key: string]: any }>): stream.PassThrough & AsyncIterable<ArrayMember<T>>;

@monotykamary Could you also add a test for the stream.Passthrough as well? Probably something like

// $ExpectType PassThrough
knexInstance<User>('users').select('*').stream().pipe(new PassThrough());

I forgot to add this in previous PR which caused the issue.

Comment thread types/test.ts
Adds upon knex#4377 by intersecting the
`stream.PassThrough` class to maintain the stream type.
Comment thread types/index.d.ts Outdated

@nguymin4 nguymin4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me!

Please fix the test. Probably missing import.

@kibertoad kibertoad merged commit 767f32e into knex:master Mar 26, 2021
@kibertoad

Copy link
Copy Markdown
Collaborator

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants