TypeScript type inference issue when function returns function -


i have following example functions (compose ramda):

    declare function compose<v0, t1, t2>(fn1: (x: t1) => t2, fn0: (x0: v0) => t1): (x0: v0) => t2;      interface user {       id: number     }      function fn1(input: any): user {       return {id: 1};     }      function fn2<i, o>(fn: (i: i) => o): (i: i) => o {        return (data) => {          try {            return fn(data);          } catch (e) {            return undefined          }       };     } 

when tried use this:

compose(fn2(user => user.id), fn1); 

typescript throws following error:

ts2339:property 'id' not exist on type '{}'.

does know, should typescript infer proper type user?

of course, following code work:

compose<any, user, number>(fn2(user => user.id), fn1); 

it looks parametric types resolved left right , because writing any function first have error. @ code similar functions reversed:

declare function compose<v0, t1, t2>(fn0: (x0: v0) => t1, fn1: (x: t1) => t2,): (x0: v0) => t2;  interface user {   id: number }  function fn1(input: number): user {   return {id: 1}; }  function fn2<i, o>(fn: (i: i) => o): (i: i) => o {    return (data) => {      try {        return fn(data);      } catch (e) {        return undefined      }   }; }  compose(fn1, fn2(user => user.id)); 

it works expect. compose definition must changed.

edit

similar titian cernicova's answer, can define @ least first parametric element goes compose chain this:

declare function compose<t1 = any, t2 = any, v0 = any>(fn1: (x: t1) => t2, fn0: (x0: v0) => t1): (x0: v0) => t2;  interface user {   id: number }  function fn1(input: number): user {   return {id: 1}; }  function fn2<i, o>(fn: (i: i) => o): (i: i) => o {    return (data) => {      try {        return fn(data);      } catch (e) {        return undefined      }   }; }  compose<user>(fn2(user => user.id), fn1); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -