-
Notifications
You must be signed in to change notification settings - Fork 22
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
addIndex #6
Comments
Played around with this a bit and ended up with this: https://tsplay.dev/mqq5jm Its not perfect (eg; auto type inference confuses Also its entirely possible this is already well known, but while working on this I thought of an approach to defining overloads that return generic functions with multiple curried call signatures: declare function indexedReduceSignature<T, R>(
callback: IndexedReduceCallback<T, R>,
acc: R,
list: T[],
): R;
declare function indexedReduceSignature<T, R>(
callback: IndexedReduceCallback<T, R>,
acc: R,
): (list: T[]) => R;
declare function indexedMapSignature<T, N>(
callback: IndexedMapCallback<T, N>,
list: T[],
): N[];
declare function indexedMapSignature<T, N>(
callback: IndexedMapCallback<T, N>,
): (list: T[]) => N[];
/**
* Reduce
*/
export function myAddIndex(
fn: (
callback: NonIndexedReduceCallback<any, any>,
acc: any,
list: any[],
) => any,
): typeof indexedReduceSignature;
/**
* Map
*/
export function myAddIndex(
fn: (callback: NonIndexedMapCallback<any, any>, list: any[]) => any[],
): typeof indexedMapSignature; By defining the different possible output shapes as their own functions with their own overloads, and then referencing them with Edit: The approach I described is not actually as powerful as I though, I tried applying it to the |
Draft branch: https://github.com/ramda/types/tree/addIndex
Current issues
addIndex
is particularly unique in terms of type definitions because of how the return type depends on the given functionLet's use filter as an example
To actually use it...
The problem here is that you have to declare the generic for the type of the
Array
you intend to use it for. This defeats the purpose of generics in general, however, not passing the generic means that the type isunknown
, which means the return type for all use cases isunknown[]
That at least lets you use
filterIndexed
for any type, but then you lose all type inference. Not just for the thelist
, but for thepredicate
as wellisEven
also needs to beisEven = (item: number, i: number, list: number[]) => boolean
otherwise it errors as well. This means that the innerfn
type needs to support all overloadsFix proposals
TODO
The text was updated successfully, but these errors were encountered: