Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@smartinio
Copy link
Contributor

@smartinio smartinio commented May 23, 2023

What?

Adds support for inferred pipeline types like this:

image

How?

  • A generic type representation of the commands array is stored on the Pipeline class itself
  • The type of .chain is then updated to return a Pipeline type with whatever command was just chained fed into the class-level generic
  • Finally, the type of .exec is updated to return the type of the commands array mapped to each item's response type. Kept the empty case as unknown[] for backward compatibility.

Gotchas

TS thinks that each .chain call returns a new Pipeline instance, which means that the type inference only works if the chain is kept intact from pipeline() to exec(). Splitting the pipeline up for use in loops etc will still require manually overriding exec<['like', 'this']>

const p = redis.pipeline()

for (const x of [1,2,3]) {
  p.set('foo', 'bar')
  p.get('baz')
}

const result = await p.exec() // TS: const result: unknown[]

@smartinio smartinio marked this pull request as ready for review May 23, 2023 21:28
@chronark
Copy link
Contributor

closing in favor of #355 because the CI won't run on foreign PRs due to secrets involved :/

@chronark chronark closed this May 31, 2023
@smartinio smartinio deleted the feature/infer-chained-response-types-in-pipeline branch May 31, 2023 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants