-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Feature request: represent Function via generic type parameterized by its return/parameter/this type #12342
Comments
See also #6606 |
In your specific example, wouldn't declare interface Vue {
new <T extends {[k: string]: Function}>(config: {computed: { [K in keyof T]: () => T[K]}}): T
} Be an equivalently correct type (without need for a special type function), provided T were inferred correctly? |
@weswigham I believe T could not be inferred correctly for now by design: mapped type is not an inference site. #12114 (comment) |
@HerringtonDarkholme note the comment:
Accumulating valuable use cases, such as this, is how things like this can change. 👍 |
Tracking this as well, wanna type |
That's going to be a great step towards generalization of functions. I like it. However, it would not be enough. Next things to consider are:
|
I happen to need full on return type rewriting for my particular use case. To pull from #12381, I need a transformation from interface Original<T> {
[P in keyof T]: (...args) => R;
}
interface Wrapped<T> {
[P in keyof T]: (...args) => Promise<R>;
} |
What is the status of this? It seems to me like adding the proposed Below is an example of a function that cannot be given a correct type without this feature (note that I use type ApplyObject<O extends Record<string, Pair>> = {
[K in keyof O]: ReturnType<O[K]>
}
// Takes an object of functions, calls each function and returns an object with the results
function applyObject<O extends { [p: string]: () => any }>(object: O): ApplyObject<O> {
const newObject: any = {};
for (const key of Object.keys(object)) {
newObject[key] = object[key](); // <- Note application here
}
return newObject;
} |
How about instead, create a new type-level operator (instead of a native generic) called type ApplyObject<O extends Record<string, Pair>> = {
[K in keyof O]: returnof O[K]
} It fits better with the theme of using syntax instead of generics for primitive type-level operations. |
I think this usage is already covered by conditional type. Closing. |
Yeah. For reference, see this |
Background:
With #11929, we can get property type of an object. But we cannot get the return/parameter/this types of a function at compile time.
For single function argument, getting return/parameter type is relative simple by adding generic parameter. But when API is designed to accept a map of functions, it become hard to express in TypeScript.
Using function is a common pattern in vuejs and its derivation.
By combining mapped types and
keyof
, we can achieve some compile time types to capture this pattern.If only we have a special type called
ReturnType
.Another usage is for event handling in most frontend framework. For example, backbone has an event-map syntax for event binding.
Also, with return/parameter/this type support, we can achieve typed bind/apply/call
Note, this would require variadic generic types.
Proposal:
For every function type, we can have a special access key for return type/ parameter type. Parameter type is a tuple type for every argument. If function has rested parameter, parameter type will fallback to array type.
For overloaded function, the return type/parameter type is the union of all corresponding type of overloading signature.
For generic type parameter with function type constraint, the return type is resolved to constraint. the parameter type also resolves to constraint's parameter type (not regarding with variance.) If generic type has no constraint, resolve it to
{}
.For
Function
, the return and parameter type isany
, the parameter type isArray<any>
.To access function's parameter type, it might be good to reuse index access type. For example
Related
#212
#1773
Edit: I think representing function via typed generic is a more complete solution. But it requires more typing features as prerequisites and rewriting all function type encoding.
The text was updated successfully, but these errors were encountered: