最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

inheritance - How to extend typescript interface with specific values enforced in an array property - Stack Overflow

programmeradmin5浏览0评论

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:

  1. Defining ExtendedInterface using extends Omit<Interface, 'values'> and redefining values - This defies proper inheritance and disallows passing ExtendedInterface instances as a parameter to functions accepting Interface
  2. 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?

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:

  1. Defining ExtendedInterface using extends Omit<Interface, 'values'> and redefining values - This defies proper inheritance and disallows passing ExtendedInterface instances as a parameter to functions accepting Interface
  2. 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?

Share Improve this question asked Mar 18 at 16:36 TomTom 2251 gold badge3 silver badges7 bronze badges 10
  • I don't understand why the open-ended tuple solution doesn't work for you (i.e., the one with ...EnforcedValue[] at the end). Closed-ended tuples like [EnforcedValue.FirstValue] have a length restriction, 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 else ExtendedInterface wouldn't be assignable to Interface. 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. – jcalz Commented Mar 18 at 17:01
  • Well the thing is that down the line I want to check what values are in the values property and if I keep it open ended, someone could put [EnforcedValue.FirstValue, EnforcedValue.SecondValue] even into an object of type Interface, which should not happen. I want to be able to define contents of values as strictly as possible, but also be able to use inheritance, where the subtypes would add something to the values property. – Tom Commented Mar 19 at 19:59
  • Not sure how would I clarify my question more. Basically what I am trying to do is to create a runtime type checking system. Imagine many objects of many types get shuffled and since they are plain objects and not instances of any class, I cannot use instanceof to find out which is which. That is why I want to very strictly define the values, which I can then use to find out which type of object I am working with. – Tom Commented Mar 19 at 20:30
  • That is why any interface extending an 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. – Tom Commented Mar 19 at 20:43
  • You're asking for contradictory things. You do want someone to come along and put [EnforcedValue.FirstValue, EnforcedValue.SecondValue] for an object of type Interface, because if that's prevented then an ExtendedInterface would not be an Interface. Please let me know whether or not you understand this fundamental feature of TypeScript's structural type system. – jcalz Commented Mar 19 at 20:53
 |  Show 5 more comments

1 Answer 1

Reset to default 1

You could use generics:

Playground

interface ObjectWithEnforcedValues<T extends readonly EnforcedValue[] = readonly EnforcedValue[]> {
    values: T
}

enum EnforcedValue {
    FirstValue = 'FirstValue',
    SecondValue = 'SecondValue',
}


interface Interface<T extends readonly EnforcedValue[] = readonly [EnforcedValue.FirstValue]> extends ObjectWithEnforcedValues <T> {

}


interface ExtendedInterface<T extends readonly EnforcedValue[] = readonly [EnforcedValue.FirstValue, EnforcedValue.SecondValue]> extends Interface<T> {
    
}


const i1: Interface = {values: [EnforcedValue.FirstValue]}
const i2: ExtendedInterface = {values: [EnforcedValue.FirstValue, EnforcedValue.SecondValue]}

To ensure structural inheritance you could use an object instead of an array:

Playground

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论