You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Just logging a quick issue in case I don't get to a PR) Some of the methods use weird overloads instead of generics and then call generics, this can make it super painful to make code that wraps these methods with the right types without type errors.
The quintessential example is in the generate code:
This is due to ambiguity in which overload signature should be used.
Things get even worse if you want to make stream generic so the right return type can be passed - you have to do different calls for the different generic cases and cast the result to a type conditional.
The overloading works well for simple use cases but generics shouldn't make a difference for the simple uses while making types much easier to work with on more complex ones.
The text was updated successfully, but these errors were encountered:
Just been looking at this again and it is not as straight-forward as I had assumed (using conditionals like @Pixelycia mentioned).
You can do it through conditionals but you have to type cast from the specific case to the conditional case every time you return from a method which becomes quite messy.
Another way to get types to behave correctly in most cases is to add a ton of signatures:
This behaves correctly in cases a-e but fails in case f.
Both adding more signatures or lots of ugly type conditional casts is messy, could we consider splitting the stream cases into separate methods?
If we did something like ollama.stream.generate, we could get types to work in all cases and get rid of that weird return type: Promise<AsyncGenerator<GenerateResponse>>.
Another possibility is to make streaming the default case and export a helper to convert the stream to a single value like:
(Just logging a quick issue in case I don't get to a PR) Some of the methods use weird overloads instead of generics and then call generics, this can make it super painful to make code that wraps these methods with the right types without type errors.
The quintessential example is in the generate code:
ollama-js/src/browser.ts
Lines 109 to 121 in dfc7988
The first issue is this makes the stream parameter un-assignable to boolean, meaning this code has a type error:
This is due to ambiguity in which overload signature should be used.
Things get even worse if you want to make stream generic so the right return type can be passed - you have to do different calls for the different generic cases and cast the result to a type conditional.
The overloading works well for simple use cases but generics shouldn't make a difference for the simple uses while making types much easier to work with on more complex ones.
The text was updated successfully, but these errors were encountered: