-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow generic parameters to propagate on returned type references. #23955
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
Comments
The syntax So What am I missing here? |
I am aware, thanks. That was a simple example. The second code much better explains it. In essence I'm looking for this:
Which of course does not work - I would need the type of generic To clarify, in my second code, the The case for #17574 is different from mine (the OP is talking about using |
It still seems there's a disconnect about the difference between values and types when it comes to classes. You can't do You can do I guess your problem isn't that // doesn't work
type GenericInstanceType<C extends new <R>(...args: any[]) => any, T> =
C extends new <R>(...args: any[]) => (infer F)<R> ? F<T> : never; where the // also doesn't work
export interface ITest<Tx> extends InstanceType<TestType, Tx> { } So maybe this issue is a request for higher kinded types in TypeScript? Or specifically, some way to refer programmatically to the stuff going on in the |
JCalz, I hoped it was clear I know that by now. Of course you can't that's the point. ;) I was only trying to give an idea of what I was thinking, that is all.
Correct, that's my main point. ;) You made a good point about being able to use a class to inherit from a generic type, in which I could create an interface type from the class type:
I think it may be an acceptable workaround, for now. 👍 :) Of course I shouldn't have to do this in the first place. Perhaps it is a request for higher order types, not sure either. All I know is my idea is very simple to lay out. If I had this:
Means 'A' references a constructor AND a generic class type. Then if I do this:
|
If you're saying that At this point I should bow out and wait to see what real TypeScript language designers/maintainers have to say, since I'm just an |
We've actually been considering binding const MyClass = module.exports = class Foo {} and then referring to |
We currently have no way of letting generic parameters "escape" from an instantiation. the system assume that all parameters will be accounted for. i |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I believe this issue should be considered higher priority. Generic classes should not be losing type information. Playground link Related: #13798 My use caseDeclaring/returning a generic class in an IIFE, which also contains other |
Seems like not just generic type is lost but constructor too export function Wrapper() {
return class {
title!: string;
};
}
function bindProperty<Prop extends ReturnType<typeof Wrapper>>(Prop: Prop) {
class SyncedProp extends Prop {} // <-- why the Prop is NOT a constructor type?
// Type 'Prop' is not a constructor function type.ts(2507)
} |
Search Terms
Generic type parameters are lost upon return from a function.
Suggestion
In the case of the following:
(https://goo.gl/YefQ6z)
I would have expected that the type of
t
istypeof Test<Tx>
.Func()
might be considered as a type of decorator of sorts, but it is a lot more complicated in my actual code, but this gets the idea across.Use Cases
I'm creating a factory type system that is wrapped by a function (i.e.
export Type = CreateFactory(base, class SomeType<T>(){...})
). It uses type inference to figure out types and adds other types to the return type before exporting a property. Similar to:(https://goo.gl/fMtCPQ)
This works OK, and there is no issue with non-generic types. The problem is now with
A.Test.originalType
. I have no way to access the generic type signature anymore. Instead, I simply gettypeof Test
. I recommend the generic type signatures propagate properly. It may even help some people catch more bugs. This should allow me to do this:Note: My actual code is a bit more complicated and allows inheritance chaining of the factories, which is why this pattern was the best suited for it.
Checklist
My suggestion meets these guidelines:
[x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[x] This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered: