microsoft / microsoft/TypeScript

Ability to get generic type from typeof and infer

Offen
#33,185 6 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Needs Proposal Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die generic-function- und React-Beispiele im Issue zu reproduzieren, und prüfe die zugehörige Diskussion in #32170. Da keine Repository-Dateien oder Tests genannt werden, erfordert das Auffinden der type-query- und inference-Einstiegspunkte eine Untersuchung der Codebasis. Als erledigt gilt die Unterstützung der generischen Instanziierung über typeof und infer bei gleichzeitiger Beibehaltung der demonstrierten Argumenttypen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.