#13487 added default generic types, but it's still not possible to infer a generic type:
typeReturn<Textends()=>S,S=any>=S
Here S takes its default type any, but T could permit inference of a subset type of any for S:
constHello=()=>'World'typeHelloReturn=Return<typeofHello>// any
Here HelloReturn still has any type, and TypeScript could infer S as the literal type 'World', which is a subset of any.
Use Case
Here's an example of a fully declarative workaround for #6606, using generic type inference:
typeReturn<Textends()=>S,S=any>=S
constHello=()=>'World'typeHelloReturn=Return<typeofHello>// 'World'
Default Type
If return of T is not a subset of S, it should throw an error:
typeReturnString<Textends()=>S,S=string>=S
constHello=()=>'World'typeHelloReturn=ReturnString<typeofHello>// 'World'
constcalculateMeaningOfLife=()=>42typeMeaningOfLife=ReturnString<typeofcalculateMeaningOfLife>// ERROR: T should return a subset of String
Multiple Possible Type Inferences
The current implementation always returns the superset (which is just the default generic type), solving this issue would require to return the subset (the most precise type of all the inferred possibilities).
If a type has multiple possible type inferences, TypeScript should check that all these types are not disjoint, and that they all are subsets of the Default Generic Type.
The inferred type is the most precise subset.
#13487 added default generic types, but it's still not possible to infer a generic type:
Here
Stakes its default typeany, butTcould permit inference of a subset type ofanyforS:Here
HelloReturnstill hasanytype, and TypeScript could inferSas the literal type'World', which is a subset ofany.Use Case
Here's an example of a fully declarative workaround for #6606, using generic type inference:
Default Type
If return of
Tis not a subset ofS, it should throw an error:Multiple Possible Type Inferences
The current implementation always returns the superset (which is just the default generic type), solving this issue would require to return the subset (the most precise type of all the inferred possibilities).
If a type has multiple possible type inferences, TypeScript should check that all these types are not disjoint, and that they all are subsets of the Default Generic Type.
The inferred type is the most precise subset.