addIndex
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- developer-experience
Research direction
Start by reviewing the addIndex and filter TypeScript definitions on the linked addIndex draft branch, then reproduce the generic-inference examples from this issue. Done means addIndex can infer the array and predicate types without an explicit element-type generic while preserving type errors for incompatible inputs.
Written by the indexing model from the issue text.
Description
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 function
Let's use filter as an example
// Special case for filter
export function addIndex<T>(
fn: (f: (item: T) => boolean, list: readonly T[]) => T[],
): _.F.Curry<(a: (item: T, idx: number, list: T[]) => boolean, b: readonly T[]) => T[]>;
To actually use it...
const filterIndexed = addIndex<number>(filter);
filterIndexed(predicate, [1, 2, 3, 4]); // ok
filterIndexed(predicate, ['a', 'b', 'c', 'd']); // error
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 is unknown, which means the return type for all use cases is unknown[]
const filterIndexed = addIndex(filter);
filterIndexed(predicate, [1, 2, 3, 4]); // returns `unknown[]`
filterIndexed(predicate, ['a', 'b', 'c', 'd']); // returns `unknown[]`
That at least lets you use filterIndexed for any type, but then you lose all type inference. Not just for the the list, but for the predicate as well
addIndex<string>(filter)(isEven, ['a', 'b', 'c', 'd']); // error on isEven, because `(a: number) => boolean !== (a: string) => boolean`
addIndex(filter)(isEven, ['a', 'b', 'c', 'd']); // no error, because everything here is `unknown`
isEven also needs to be isEven = (item: number, i: number, list: number[]) => boolean otherwise it errors as well. This means that the inner fn type needs to support all overloads
Fix proposals
TODO
- Dominant language
- TypeScript
- Stars
- 31
- Forks
- 27
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ramda/types
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
`intersection` should return an array of the intersection of the types of each array's elementsOpen
Difficulty 3/5 1-2 days Newbie friendliness 65/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
openzim/mwoffliner#2933 ·
Maintainers usually reply within 1 day
-
Use the README category name for website links and submissionsPossibly taken @dajiaohuang claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
birobirobiro/awesome-shadcn-ui#647 ·
Maintainers usually reply within 2 days
-
check:passed streams:add
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Urigo/accounter-fullstack#4604 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day