You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Assume type X = <T>() => Z<T>; as having an optionnal generic type parameter to enable deducing the return type on generic methodsΒ #57102
changed the title [-]Considering `type X = <T>() => Z<T>;` as having an optionnal generic type parameter to enable deducing the return type on generic methods[/-][+]Assume `type X = <T>() => Z<T>;` as having an optionnal generic type parameter to enable deducing the return type on generic methods[/+]on Jan 20, 2024
I'd interpret this as a request for "instantiation types" as opposed to instantiation expressions as implemented in #47607 (which cannot directly be used to get this functionality, as you note above, and as mentioned in #47607 (comment)). Also cross-linking #53410
I'd interpret this as a request for "instantiation types" as opposed to instantiation expressions as implemented in #47607 (which cannot directly be used to get this functionality, as you note above, and as mentioned in #47607 (comment)). Also cross-linking #53410
Thanks for your answer.
I read in #47607 (comment)) that the syntax T<X> is a bad idea as it would be ambiguous :
At least, if we did, it wouldn't be immediately obvious whether T means "apply type arguments to generic type T" or "apply type arguments to generic signatures in type T (without instantiating T itself)".
I would disagree with that, but if it is an issue, then, why not a AsGeneric utility function ?
type Foo = <T>() => T is the concrete, non-generic type of a generic function.
Does number apply to T or U here? Both interpretations are valid, because you can instantiate Foo without explicitly providing T. Therefore the above could be interpreted two ways:
T defaults to string and U = number. Bar resolves to concrete function type (x: string) => number.
T = number. Bar resolves to generic function type <U>(x: number) => U.
Does number apply to T or U here? Both interpretations are valid, because you can instantiate Foo without explicitly providing T. Therefore the above could be interpreted two ways:
1. `T` defaults to `string` and `U = number`. `Bar` resolves to concrete function type `(x: string) => number`.
2. `T = number`. `Bar` resolves to generic function type `<U>(x: number) => U`.
Knowing that we can write Foo<>, then why not considering :
{classBaseClass<V>{protectedfake(): V{thrownewError("")}}classKlass<V>extendsBaseClass<V>{}typeConstructor<PextendsBaseClass<number>>=new()=>P;typeinferTest<T>=TextendsConstructor<infer P> ? P :never;typeU=inferTest<Constructor<Klass<number>>>// U = Klass<number>}
But when we add a generic type parameter :
{classBaseClass<V>{protectedfake(): V{thrownewError("")}}classKlass<V>extendsBaseClass<V>{}typeConstructor<V,PextendsBaseClass<V>>=new()=>P;typeinferTest<V,T>=TextendsConstructor<V, infer P> ? P :never;typeU=inferTest<number,Constructor<number,Klass<number>>>// U = BaseClass<number>}
I tried to do a minimal reproducible example, and I think I found the issue.
Functions return type are computed only once, when declaring the function, however the return type isn't precise enough.
For example :
{classBase{getself(): Base{returnthis}}classChildextendsBase{overridegetself(): Child{returnthis}}functionconvert<TextendsBase>(X: T){returnX.self;}constvalue=convert(newChild());// expected: typeof value = Child.// got: typeof value = Base.}
convert return type is : Base when it should be : T["self"] (which extends Base).
However, this would cause issue when the method/attribute has a generic, requiring to use AsGeneric<T["self"], U>.
Maybe a condensed notation could be something like : T["self"<U>].
Duplicate #50481 and others. The proposed solution isn't really tractable IMO; it's very problematic for the same syntax to do two completely different things.
Duplicate #50481 and others. The proposed solution isn't really tractable IMO; it's very problematic for the same syntax to do two completely different things.
What about the other suggestion : type T<U> = AsGeneric<Foo, U> ?
I don't know what syntax would be the best, but we do need such feature as there is currently no workaround for it.
Activity
[-]Considering `type X = <T>() => Z<T>;` as having an optionnal generic type parameter to enable deducing the return type on generic methods[/-][+]Assume `type X = <T>() => Z<T>;` as having an optionnal generic type parameter to enable deducing the return type on generic methods[/+]denis-migdal commentedon Jan 20, 2024
It seems there is a workaround if we know the class :
However, if we don't know the class :
jcalz commentedon Jan 20, 2024
I'd interpret this as a request for "instantiation types" as opposed to instantiation expressions as implemented in #47607 (which cannot directly be used to get this functionality, as you note above, and as mentioned in #47607 (comment)). Also cross-linking #53410
denis-migdal commentedon Jan 20, 2024
Thanks for your answer.
I read in #47607 (comment)) that the syntax
T<X>is a bad idea as it would be ambiguous :I would disagree with that, but if it is an issue, then, why not a
AsGenericutility function ?Solution:
fatcerberus commentedon Jan 20, 2024
That isn't just academic wheel-spinning, for what it's worth. It's a real issue, e.g. what would TS do here?
Does
numberapply toTorUhere? Both interpretations are valid, because you can instantiateFoowithout explicitly providingT. Therefore the above could be interpreted two ways:Tdefaults tostringandU = number.Barresolves to concrete function type(x: string) => number.T = number.Barresolves to generic function type<U>(x: number) => U.denis-migdal commentedon Jan 20, 2024
Knowing that we can write
Foo<>, then why not considering :denis-migdal commentedon Jan 20, 2024
I found it strange so I made some tests :
But when we add a generic type parameter :
Is this a bug or an intended behavior ?
denis-migdal commentedon Jan 21, 2024
I tried to do a minimal reproducible example, and I think I found the issue.
Functions return type are computed only once, when declaring the function, however the return type isn't precise enough.
For example :
convertreturn type is: Basewhen it should be: T["self"](which extendsBase).However, this would cause issue when the method/attribute has a generic, requiring to use
AsGeneric<T["self"], U>.Maybe a condensed notation could be something like
: T["self"<U>].denis-migdal commentedon Jan 22, 2024
Another solution would be to make use of the
satisfieskeyword on types :RyanCavanaugh commentedon Jan 22, 2024
Duplicate #50481 and others. The proposed solution isn't really tractable IMO; it's very problematic for the same syntax to do two completely different things.
denis-migdal commentedon Jan 22, 2024
What about the other suggestion :
type T<U> = AsGeneric<Foo, U>?I don't know what syntax would be the best, but we do need such feature as there is currently no workaround for it.
typescript-bot commentedon Jan 25, 2024
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.