-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to get generic type from typeof and infer #33185
Comments
You can kind of get this with some dummy code that appears at runtime, thanks to the recent support for higher order function inference. Note that I'm going to use a non-react-specific example here. Say you have a generic function like this: declare function someGenericFunction<T>(
a0: (cb: string) => number,
a1: (cb: Array<T>, someNum: number, someVal: T) => number,
a2: (cb: T) => Array<T>
): string; and you want some way to represent the argument callbacks' arguments without having the declare function params<A extends any[]>(f: (...a: A) => any): () => A;
class SomeGeneric<T> {
args = params(someGenericFunction)<T>();
} And suddenly the types you want are available: type A0Params<T> = Parameters<SomeGeneric<T>["args"][0]>; // [string]
type A1Params<T> = Parameters<SomeGeneric<T>["args"][1]>; // [T[], number, T]
type A2Params<T> = Parameters<SomeGeneric<T>["args"][2]>; // [T] And you can fill in the type ConcreteA1Params = A1Params<boolean>; // [boolean[], number, boolean] The type of |
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 // Proposal (not working today)
type Foo = ReturnType<typeof something, number>; |
Also ran into this trying to get the parameters of a default import function. |
Just ran into this problem the other day. How could you possibly have |
With #47607 it is possible to refer to specific instantiations of generic functions. |
Somewhat related, and I did extensive Googling, I just can't find how to apply a generic parameter default type when using typeof. Am I missing something basic?
Without passing the generic parameter type 'div', you get the generic function, which results in Omit<any, 'ref'> an no props being checked at all.
|
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,
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 thatReact.SyntheticEvent
is a generic type, which allows narrowing ofe.currentTarget
.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: