microsoft / microsoft/TypeScript
Adding an overload breaks inference of compositional functions
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Tried fixing this, but ran into another case where an overload breaks type inference:
declare function reverse(a: ReadonlyArray<number>): ReadonlyArray<number>;
declare function incrementEvery(list: ReadonlyArray<number>): ReadonlyArray<number>;
declare function incrementEvery(x: ReadonlySet<number>): ReadonlySet<number>;
declare function compose<T>(
second: (x: ReadonlyArray<number>) => T,
first: (x0: ReadonlyArray<number>) => ReadonlyArray<number>,
): (x0: ReadonlyArray<number>) => T;
const works = compose<ReadonlyArray<number>>(incrementEvery, reverse);
const broke: (nums: ReadonlyArray<number>) => ReadonlyArray<number> = compose(incrementEvery, reverse);
src/a.ts(12,79): error TS2345: Argument of type '{ (list: ReadonlyArray<number>): ReadonlyArray<number>; (x: ReadonlySet<number>): ReadonlySet<num...' is not assignable to parameter of type '(x: ReadonlyArray<number>) => ReadonlySet<number>'.
Type 'ReadonlyArray<number>' is not assignable to type 'ReadonlySet<number>'.
Property 'has' is missing in type 'ReadonlyArray<number>'.
Unlike the above issue, this was broken in all the way back in 2.0.
Originally posted by @andy-ms in https://github.com/microsoft/TypeScript/issues/23352#issuecomment-380844184
Contributor 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.
Research direction
Start with the minimal reproduction in src/a.ts, comparing the inferred compose call with the explicit generic invocation. Investigate why the overloaded incrementEvery is inferred as the wrong function type; done means the unannotated compose call is accepted with the stated function type without regressing the overload cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100