When making higher order functions that return functions, type parameters are not preserved in return type. Notice in the following example how identityM has the generic type parameter A changed into {}, resulting in a loss of type safety in the rest of the code.
Ideally identityM would have the type <A>(a: A) => A. If this isn't possible, issuing a compiler warning (or error) about loss of genericity would be very helpful in tracking down errors like this.
functionmirror<A,B>(f: (a: A)=>B): (a: A)=>B{returnf;}functionidentity<A>(a: A): A{returna;}varidentityM=mirror(identity);// type: (a: {}) => {}varx=1;vary=identity(x);// type: numbervarz=identityM(x);// type: {}
When making higher order functions that return functions, type parameters are not preserved in return type. Notice in the following example how
identityMhas the generic type parameterAchanged into{}, resulting in a loss of type safety in the rest of the code.Ideally
identityMwould have the type<A>(a: A) => A. If this isn't possible, issuing a compiler warning (or error) about loss of genericity would be very helpful in tracking down errors like this.