microsoft / microsoft/TypeScript
Ability to get generic type from typeof and infer
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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 * as React from "react";
function dragHelper<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 */
}
};
}
type Args<T extends (...args: any) => any> = T extends (...args: infer A) => ReturnType<T> ? A : void;
type Move = typeof dragHelperReact extends (down: infer M, move: infer D, up: infer U) => void ? M : never;
type Down = typeof dragHelperReact extends (move: infer M, down: infer D, up: infer U) => void ? D : never;
type Up = typeof dragHelperReact extends (move: infer M, down: infer D, up: infer U) => void ? U : never;
// these work
type MoveArgs = Args<Move>;
type UpArgs = Args<Up>;
// I want to do something like this; it does not work.
type DownArgs<T> = Args<Down<T>>;
// This is a workaround I tried; it does not work either.
type DownArgs<T> = Down extends (e: any, ...args: infer U) => void ? [React.MouseEvent<T> | React.TouchEvent<T>, ...U] : never;
// This workaround works.
type DownArgs<T> = Down extends (e: any, ...args: infer U) => void ? [React.MouseEvent<T> | React.TouchEvent<T>, U[0], U[1], U[2]] : never;
class Example extends React.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:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the generic-function and React examples in the issue and review the related discussion in #32170. No repository files or tests are named, so locating the type-query and inference entry points will require codebase research. Done means supporting generic instantiation through typeof and infer while preserving the demonstrated argument types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100