I have a basic interface, which I use down the line to enforce specific values on other interfaces:
interface ObjectWithEnforcedValues {
values: readonly EnforcedValue[];
}
enum EnforcedValue {
FirstValue = 'FirstValue',
SecondValue = 'SecondValue',
}
Then I use it on an actual interfaces I want to use to create actual objects, for example:
interface Interface extends ObjectWithEnforcedValues {
values: readonly [EnforcedValue.FirstValue];
}
So far so good, but the problem comes when I try to extend the Interface and increase the number of enforced values. Typescript does not like this at all:
interface ExtendedInterface extends Interface {
values: readonly [EnforcedValue.FirstValue, EnforcedValue.SecondValue]
}
Basically the goal is to make sure anyone creating new instances of objects using these interfaces sets the values property to the specific enforced values, but I am not able to make it work properly with inheritance. Logically it seems that it SHOULD work, because in the case above, Interface enforces the first value in the array to be FirstValue and ExtendedInterface adheres to that and only adds a SecondValue to the second position of the array. Is there any way to do this or Typescript just does not allow this? First thing that comes to mind is using class and just set the property directly there instead of using interfaces like this, but unfortunately I cannot use that, everything is just plain objects.
Things I have tried:
- Defining
ExtendedInterfaceusingextends Omit<Interface, 'values'>and redefiningvalues- This defies proper inheritance and disallows passingExtendedInterfaceinstances as a parameter to functions acceptingInterface - Defining interfaces in question like this:
interface Interface extends ObjectWithEnforcedValues {
values: readonly [EnforcedValue.FirstValue, ...EnforcedValue[]];
}
interface ExtendedInterface extends Interface {
values: readonly [EnforcedValue.FirstValue, EnforcedValue.SecondValue,
...EnforcedValue[]]
}
This makes more or less everything work as I need, but allows to put whatever values at the end of the array, which I do not want to allow.
Is there any way to make it work properly or should I just give up and use the second option?
...EnforcedValue[]at the end). Closed-ended tuples like[EnforcedValue.FirstValue]have alengthrestriction, so you can't "extend" it by adding stuff to the end. Conversely, open-ended tuples allow you to "put whatever values at the end of the array" which you do want to allow, or elseExtendedInterfacewouldn't be assignable toInterface. It's very unclear to me what you mean by "extend". Please edit to clarify your use cases to make sense; right now it's contradictory.valuesproperty and if I keep it open ended, someone could put[EnforcedValue.FirstValue, EnforcedValue.SecondValue]even into an object of typeInterface, which should not happen. I want to be able to define contents ofvaluesas strictly as possible, but also be able to use inheritance, where the subtypes would add something to thevaluesproperty.class, I cannot useinstanceofto find out which is which. That is why I want to very strictly define thevalues, which I can then use to find out which type of object I am working with.extendingan existing interface should contain all values coming from sub interfaces plus one more for the new interface to make it work like usual inheritance. And that is why it is desirable that nobody can freely append stuff at the end of the array for the given type.[EnforcedValue.FirstValue, EnforcedValue.SecondValue]for an object of typeInterface, because if that's prevented then anExtendedInterfacewould not be anInterface. Please let me know whether or not you understand this fundamental feature of TypeScript's structural type system.