-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Cannot use differentiating property with generics and inheritance #44733
Comments
When going through the conditional type You could use some sort of counterfactual reasoning of the sort "No legal subtype of MyClassV1 could have a more-more specific type than a unit type", but that assumption precludes further subdivision of |
I thought I understood this, but:
class MyClassV2 extends MyClassV1 {
readonly version: 'v2' = 'v2';
} gives me this error:
What would a "more-specific value of
I assume my reasoning above is exactly what you mean by this. But how is it counterfactual? As far as I understand, as of today That said, assuming that the way I am trying to achieve this just is incorrect, are there any alternatives? |
There's no ambiguity what
See #33038
Probably the best I could suggest is class GenericClassBase<T extends MyClassBase, U extends MyClassBase> {
constructor(public arg: TypeByVersion<VersionOf<U>>) {}
}
new GenericClassBase('a'); // <----- works as expected ✔
new GenericClassBase('b'); // <----- works as expected ✔
new GenericClassBase('c'); // <----- works as expected ✔
class GenericClassV1Ext<T extends MyClassV1> extends GenericClassBase<T, MyClassV1> {
constructor() {
super('a'); // <----- works as expected ✔
super('b'); // <----- fails as expected ✔
super('c'); // <----- works as expected ✔
}
} |
Bug Report
In my code I want to differentiate allowed types based on a differentiating property (
version
in my example).Some classes have this property set, other (generic) classes should accept certain values based on the classes with this property.
This works, as long as I don't introduce hierarchies between the generic classes.
With hierarchies, only values that relate to all differentiating properties (i.e., common values) can be set (see code below).
Other values fail with:
🔎 Search Terms
generics, inheritance
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Calling the super constructor from
GenericClassV1Ext
with'a'
fails.🙂 Expected behavior
I would expect calling the super constructor from
GenericClassV1Ext
with'a'
works, because it works for both other cases:GenericClassV1<T extends MyClassV1>
GenericClassBase<T extends MyClassBase>
The text was updated successfully, but these errors were encountered: