question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to get generic type from typeof and infer

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ahejlsbergcommented, Jan 27, 2022

With #47607 it is possible to refer to specific instantiations of generic functions.

4reactions
suchipicommented, Jan 23, 2020

Ran into this, too:

function something<T>() {
    const array: T[] = [];
    return array;
}

// Doesn't work; syntax error
type Foo = ReturnType<typeof something<number>>;

I don’t know how to best handle specific instantiations of generic functions on their own (eg typeof something<number>, but maybe ReturnType and Parameters could accept optional additional type parameters for this?

// Proposal (not working today)
type Foo = ReturnType<typeof something, number>;
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: How to make generic type to infer inside a function?
Here we represent DbRequest<K> as a value with a {kind: K} property and DbResponse<K> as a value of type DbResponseMap[K] . In the ......
Read more >
Is it possible to infer the the generic type from a generic method?
As I understand it, you want the Perform method to take a generic class and return a type based on the generic class....
Read more >
Documentation - Generics - TypeScript
Using Class Types in Generics ... A more advanced example uses the prototype property to infer and constrain relationships between the constructor function...
Read more >
Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keyword to either get the type of the elements...
Read more >
How To Use Generics in TypeScript - DigitalOcean
TypeScript here is inferring the generic type from the calling code ... This way, you are able to make the resulting object have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found