-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Infer keyword should work with unused generic type parameter of a superclass #40796
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
Possible, not pretty, but still usable workaround that solves this problem on the user code side is to add a property to the base class so that it will be used by compiler for type inference. class A<T1, T2, T3> {
readonly stub: T1 = {} as T1;
}
class B<T1, T2> extends A<T1, T2, boolean> { }
type FirstAParam = B<string, number> extends A<infer T1, any, any> // FirstAParam is string
? T1
: never;
// or
type FirstAParam2 = B<string, number>['stub'] // FirstAParam2 is string |
This is working as intended. See this entry in the (somewhat outdated) FAQ. The type system is structural, not nominal; two types are the same iff they have the same structure or shape, not iff they have the same name or declaration. In your code above, the type Your workaround is the recommended way to deal with cases like this: make sure your types are structurally dependent on the generic type parameters... the easiest way being to give them members of those types, like your |
@jcalz Thank you for explanation and sorry for not reading the entire FAQ! It's a sort of sad because I have a complex scenario where one generic parameter of a class should be used later by class consumers, though there are no any properties of its type inside the class. OK, I understand, there is nothing to do except using the workaround :( BTW, in this case:
|
Thanks @jcalz |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Search Terms: infer keyword subclass superclass generic type parameter
Code
Expected behavior:
FirstAParam
isstring
.Actual behavior:
FirstAParam
isunknown
.Playground Link
The text was updated successfully, but these errors were encountered: