-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Fix the TypeScript overloading for map #401
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov Report
@@ Coverage Diff @@
## master #401 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 45 45
Lines 1896 1896
=====================================
Hits 1896 1896 Continue to review full report at Codecov.
|
Co-Authored-By: gcanti <[email protected]>
gcanti
approved these changes
Nov 18, 2019
Avaq
force-pushed
the
avaq/fix-map-type
branch
from
November 18, 2019 17:18
a694f59
to
0e28c8f
Compare
dicearr
approved these changes
Nov 19, 2019
wms
added a commit
to wms/Fluture
that referenced
this pull request
Dec 4, 2019
Although fluture-js#401 solved the case for using `map` on a `ConcurrentFutureInstance`, it broke `FutureInstance#pipe(map(...))`: ```typescript import { ConcurrentFutureInstance, FutureInstance, map } from 'fluture'; declare const x: FutureInstance<Error, number>; declare const y: ConcurrentFutureInstance<Error, number>; declare const double: (x: number) => number; const v1 = map(double)(x); // ok; FutureInstance<Error, number> const v1p = map(double)(y); // ok; ConcurrentFutureInstance<Error, number> const v2 = x.pipe(map(double)) // error ``` This could potentially be down to TypeScript selecting the wrong overload (maybe because `ConcurrentFutureInstance` is the more restrictive type?) So, I came up with this version instead, which uses a Conditional type to represent the mapper function instead. It's not ideal from an ergonomics perspective, as you end up with the whole conditional type appearing in Intellisense information. I've never been fond of that. But, it does fix (and preserve all type information) for the three forms above: ```typescript import { ConcurrentFutureInstance, FutureInstance, /*map*/ } from 'fluture'; declare function map<RA, RB>(mapper: (value: RA) => RB): <T>(source: T) => T extends FutureInstance<infer U, infer V> ? FutureInstance<U, V> : T extends ConcurrentFutureInstance<infer U, infer V> ? ConcurrentFutureInstance<U, V> : never; declare const x: FutureInstance<Error, number>; declare const y: ConcurrentFutureInstance<Error, number>; declare const double: (x: number) => number; const v1 = map(double)(x); // ok; FutureInstance<Error, number> const v1p = map(double)(y); // ok; ConcurrentFutureInstance<Error, number> const v2 = x.pipe(map(double)) // ok; FutureInstance<Error, number> ``` I did also experiment with simply swapping the order of the overloads around: ```typescript import { ConcurrentFutureInstance, FutureInstance, /*map*/ } from 'fluture'; declare function map<RA, RB>(mapper: (value: RA) => RB): { <L>(source: ConcurrentFutureInstance<L, RA>): ConcurrentFutureInstance<L, RB>; <L>(source: FutureInstance<L, RA>): FutureInstance<L, RB>; } declare const x: FutureInstance<Error, number>; declare const y: ConcurrentFutureInstance<Error, number>; declare const double: (x: number) => number; const v1 = map(double)(x); // ok; FutureInstance<Error, number> const v1p = map(double)(y); // ok; ConcurrentFutureInstance<Error, number> const v2 = x.pipe(map(double)) // almost ok; FutureInstance<unknown, number> ``` Observe that the left type of `v2` is `unknown` when it should be `Error`. I suspect this is down to that hanging `<L>` type parameter, and the only way to eliminate that (afaik) is to use a conditional type instead. Which, coincidentally, is where we came in.
This was referenced Jan 1, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To close #400
I'm planning to release this immediately as a patch.
/cc @gcanti