-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
Suggestion: allow generics to be passed into function type annotation #32973
Comments
Another idea: provide syntax to signify the annotation should be treated only as an upper bound: const fn: SubType<Fn<any>> = <T>(t: T) => t; Although you still have to awkwardly place the generic in the function definition. 👎 This syntax would not just be limited to function values either: type X = { foo: number }
const myX: SubType<X> = { foo: 1, bar: 2 };
myX.bar // number |
You want to be able to use one type to describe these two cases? //Non-generic type, generic function
type GenericFn = <T>(t : T) => T;
//Generic type, non-generic function
type ConcreteFn<T> = (t : T) => T; One more case for no real reason, //Generic type, generic function
type GenericFn2<U> = <T>(t : T) => U; |
I'm not sure that's what I'm describing, but maybe I'm just being slow. |
Your type, type Fn<T> = (t: T) => T; is the //Generic type, non-generic function
type ConcreteFn<T> = (t : T) => T; Your type, // But what if I want to propagate the generic, so that `fn` is also a generic function?
// There is no syntax for that.
// Pseudo code:
// const fn: Fn<T> = t => t; is the //Non-generic type, generic function
type GenericFn = <T>(t : T) => T; At least, if I am reading it right. And you're looking for a way to represent the above two types using just one type. |
I don't see that as related. I have a single generic type: type Fn<T> = (t: T) => T; … which I want to use to annotate a value, However, I don't want to provide a generic to // Pseudo code:
const fn: Fn<T> = t => t;
const result1 = fn(1) // number
const result2 = fn('foo') // string |
In other words, given: type Fn<T> = (t: T) => T;
const fn: (<T>(t: T) => T) = (t) => t; you're looking for a way to represent |
So, you have a single generic type, //Generic type, non-generic function
//Same as: type Fn<T> = (t: T) => T;
type ConcreteFn<T> = (t : T) => T; And you want to write the following, //Non-generic type, generic function
//Same as your pseudo code: Fn<T>
type GenericFn = <T>(t : T) => T;
const fn: GenericFn = t => t;
const result1 = fn(1) // number
const result2 = fn('foo') // string However, instead of doing it as above, which requires two type aliases, const fn: MagicallyConvertTo_NonGenericType_GenericFunction<ConcreteFn> = t => t;
const result1 = fn(1) // number
const result2 = fn('foo') // string [Edit] |
@OliverJAsh if I'm not mistaken, the second code block in @AnyhowStep's comment is exactly what you are looking for. You just need to change the definition of type Fn = <T>(t: T) => T; |
Looks like you want this #17574. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
1 similar comment
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh TS bot is borked |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
1 similar comment
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Putting the bot out of its misery. |
Search Terms
generic function expression annotation annotate
Suggestion
Related: #27124
Given a generic function type,
… if we want to use that type as an annotation, there is no way of preserving/propagating the generic—the generic must be provided.
In Elm, we can do this by passing generics into the function type annotation:
I would like to be able to do a similar thing in TypeScript.
As for what the syntax should actually look like, here's a quick (and possibly very naive) idea:
Although we would of course have to question how this annotation syntax should work for values other than functions, which can not be generic.
Use Cases
Any time we want to use a function type as an annotation, but keep the resulting type generic.
E.g. a React
HOC
type, which is generic, and when we use it to define our HOC, we want the resulting HOC type to also be generic:Another example: generic React components:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: