typescript - Declare type for a variable which contains a reference to a generic function -


in essense, instead of writing

function test<t>(param: t): t {   return param; }  test<string>("xxx"); 

i want have type declaration can applied arrow function value assigned variable.

type x<t> = (param: t) => t; const test: x<t> = (param) => param; // <- ???? test<string>("xxx") 

is possible in ts 2.5 in non-hacky way? .... or @ least in hacky way?

background: i'm trying migrate existing es6 code typescript. function implementation in es6 looks this:

import * react "react"; import * recompose "recompose"  const formenhancer = recompose.withcontext(   { form: react.proptypes.any },   function getchildcontext(props) {     const { value, errors, onchange } = props;     return { form: { value, errors, onchange }};   } ); 

value has v type, errors has ierrors<v> type , onchange has changehandler<v> type.

the expected type of function is

export type formenhancer<v> =   recompose.inferablecomponentenhancerwithprops<{}, props<v>>; 

which function interface:

export interface inferablecomponentenhancerwithprops<tinjectedprops, tneedsprops> {   <p extends tinjectedprops>(component: component<p>):     react.componenttype<omit<p, keyof tinjectedprops> & tneedsprops> } 

the tricky part can't find way add type declaration variable.

writing like

const formdecorator: formenhancer<v> = ... 

leads immediate failure, v not defined.

the best version able wrapping recompose.withcontext own explicit function

function formenhancer<v>(target: react.componenttype) {   return recompose.withcontext<icontextprops<v>, props<v>>(     { form: react.proptypes.any },     function getchildcontext(props) {       const { value, errors, onchange } = props;       return { form: { value, errors, onchange }};     }   )(target); } 

which doesn't quite solve problem i'm still able write

const raw = ({ foo }) => <input type="text" />; const wrapped = formenhancer(raw); 

which means ts not able infer props type target. being able write

function formenhancer<v>(target: react.componenttype<p extends props<v>>) { 

would helpful.

this works me:

const test = (<t>(param: t) => param); 

this pass definition of x<t> function returns same type first argument.


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 -