TypeScript version: 3.6.2
Search Terms
typeof generic function return generic type
infer arguments from generic function
Suggestion
I ran into the issue described at #32170, where the answer was
I expect what you're trying to do is refer to a specific call instantiation of a generic function, which isn't currently possible.
The feature request is to make this possible.
Examples
In the following code, dragHelperReact is a helper function for implementing draggable functionality, which also abstracts over Mouse vs Touch events (since Pointer Events are not supported in older browsers). It takes as input three callbacks; since those callbacks have fairly lengthy signatures, I want to infer their argument types. The difficulty is that React.SyntheticEvent is a generic type, which allows narrowing of e.currentTarget.
import*asReactfrom"react";functiondragHelper<T>(move: (e: MouseEvent|TouchEvent,coords: {x: number;y: number;dx: number;dy: number})=>void,down: (e: React.MouseEvent<T>|React.TouchEvent<T>,coords: {x: number;y: number},upHandler: (e: MouseEvent|TouchEvent)=>void,moveHandler: (e: MouseEvent|TouchEvent)=>void)=>void,up: (e: MouseEvent|TouchEvent)=>void){return{onMouseDown: ()=>{/* implementation */}};}typeArgs<Textends(...args: any)=>any>=Textends(...args: infer A)=>ReturnType<T> ? A : void;typeMove=typeofdragHelperReactextends(down: infer M,move: infer D,up: infer U)=>void ? M : never;typeDown=typeofdragHelperReactextends(move: infer M,down: infer D,up: infer U)=>void ? D : never;typeUp=typeofdragHelperReactextends(move: infer M,down: infer D,up: infer U)=>void ? U : never;// these worktypeMoveArgs=Args<Move>;typeUpArgs=Args<Up>;// I want to do something like this; it does not work.typeDownArgs<T>=Args<Down<T>>;// This is a workaround I tried; it does not work either.typeDownArgs<T>=Downextends(e: any, ...args: infer U)=>void ? [React.MouseEvent<T>|React.TouchEvent<T>, ...U] : never;// This workaround works.typeDownArgs<T>=Downextends(e: any, ...args: infer U)=>void ? [React.MouseEvent<T>|React.TouchEvent<T>,U[0],U[1],U[2]] : never;classExampleextendsReact.Component{down(...[e,coords,upHandler,downHandler]: DownArgs<HTMLDivElement>){// e.currentTarget is narrowed to HTMLDivElement}move(...[e,coords]: MoveArgs){}up(...[e]: UpArgs){}render(){return(<div{...dragHelper(this.move,this.down,this.up)}/>);}}Checklist
My suggestion meets these guidelines:
TypeScript version: 3.6.2
Search Terms
typeof generic function return generic type
infer arguments from generic function
Suggestion
I ran into the issue described at #32170, where the answer was
The feature request is to make this possible.
Examples
In the following code,
dragHelperReactis a helper function for implementing draggable functionality, which also abstracts over Mouse vs Touch events (since Pointer Events are not supported in older browsers). It takes as input three callbacks; since those callbacks have fairly lengthy signatures, I want to infer their argument types. The difficulty is thatReact.SyntheticEventis a generic type, which allows narrowing ofe.currentTarget.Checklist
My suggestion meets these guidelines: