-
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
Trying to get the type of a generic method of a class results in a parse error #53410
Comments
I don't know what |
The T generic of the method.
|
I don't know what you mean by "a more narrow subtype". At that point there is no more narrow subtype. The function is generic, the type is provided at call site, there's no concrete type available. |
My real world case is that the generic extends The first code snippet in this comment is what I'm trying to do, except with classes: #53318 (comment) The core issue here is that it's not possible to get the full type of a class method (as a generic). |
This is working as intended, not a bug. Instantiation expressions are expressions, not types. You can't write type X = <T>(x: T) => T[];
type Y = X<string>; // <-- errror Instead you'd need to write type X = <T>(x: T) => T[];
declare const x: X;
const y = x<string>;
type Y = typeof y; You simply must drop to value space to do this; it can't be done purely at the type level. In your code above it could look like declare const fooBar: Foo['bar'];
type Ftype = typeof fooBar<'test'> |
@jcalz Makes it clear what he actually (probably) wanted and what the meaning of the |
Good catch, @jcalz - I knew it was an attempt at an instantiation expression, but I admit I also thought it was a bug that it didn’t work. See, what’s frustrating is that you can do… type Fun<T> = (x: T) => T;
type StringFun = Fun<string>; // (x: string) => string …and that makes perfect sense, so it does ultimately seem natural to expect this kind of thing to work. It’s that pesky subtle distinction between “generic function type” and “concrete function type with generic type parameters” that gets in the way. |
It is possible to access a method type from |
Thanks, much appreciated. Yes, that was a little confusing, especially since I thought that in the example Edit: The prototype solution is not going to work if the class has a generic too. |
@mckravchyk Yeah, there’s a confusing distinction here in that |
Bug Report
🔎 Search Terms
get type of a generic class method parse error
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about it.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
I'm not able to get the type of a generic method in a class. It results in a parse error.
🙂 Expected behavior
Getting type of a generic method of a class should be possible.
Additional information
I need this to get the return type of a generic function from within to cast the return result (a workaround to to the bug with generic conditionals not resolving in certain cases).
It's not possible to just create a separate type to hold a reference to the method type because that type is not going to be a generic, and if you try to make a generic type out of it, it will result in the same problem. I'm not aware of any workarounds.
The text was updated successfully, but these errors were encountered: