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
TL;DR: When a type guard function is passed to a higher order function with generics, the type guard information is lost and simply turned into boolean.
Code
import{curry}from'lodash'// import { curry } from 'ramda' // same behaviorinterfaceValidator<T,VextendsT>{(input: T): input is V}interfaceCar{speed?: number}interfaceFastCarextendsCar{speed: number}constisFastCar=curry(function(minSpeed: number,// some configuration setting that needs to be injectedcar: Car): car is FastCar{returncar.speed!==undefined&&car.speed>=minSpeed})functionfilter<T,VextendsT>(validate: Validator<T,V>,list: T[]): V[]{constresults: V[]=[]for(constitemoflist){if(validate(item))results.push(item)}returnresults}constcars: Car[]=[{speed: 30},{},{speed: 60},{speed: 45},{speed: 0},{speed: 75}]constfastCars: FastCar[]=filter<Car,FastCar>(isFastCar(55),cars)
Expected behavior:
The isFastCar type guard information (car is FastCar) should be preserved in the curried function, as if the isFastCar function had been manually curried:
constisFastCar=(minSpeed: number)=>(car: Car): car is FastCar=>{returncar.speed!==undefined&&car.speed>=minSpeed}
fastCars would end up being [{ speed: 60 }, { speed: 75 }]
Actual behavior: Error: Argument of type 'CurriedFunction1<Car, boolean> is not assignable to parameter of type 'Validator<Car, FastCar>'. Signature '(): CurriedFunction1<Car, boolean>' must have a type predicate.
The answer may simply be "just manually curry instead of using a library", but in some real cases with longer type assertions and more arguments, manually currying like this becomes unwieldy, and it becomes impossible to supply multiple arguments or use the currying placeholder features that libraries like ramda and lodash provide.
The text was updated successfully, but these errors were encountered:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.3.4
TL;DR: When a type guard function is passed to a higher order function with generics, the type guard information is lost and simply turned into
boolean
.Code
Expected behavior:
The
isFastCar
type guard information (car is FastCar
) should be preserved in the curried function, as if theisFastCar
function had been manually curried:fastCars
would end up being[{ speed: 60 }, { speed: 75 }]
Actual behavior:
Error: Argument of type 'CurriedFunction1<Car, boolean> is not assignable to parameter of type 'Validator<Car, FastCar>'. Signature '(): CurriedFunction1<Car, boolean>' must have a type predicate.
The answer may simply be "just manually curry instead of using a library", but in some real cases with longer type assertions and more arguments, manually currying like this becomes unwieldy, and it becomes impossible to supply multiple arguments or use the currying placeholder features that libraries like ramda and lodash provide.
The text was updated successfully, but these errors were encountered: