Constraining property names in an interface that uses declaration merging (possibly via extending?)

31 views Asked by At

I am trying to use a typescript feature called "declaration merging" in order to allow my codebase to "declare" an interface in multiple files, which would then extend those key value pairs in the entire application. As shown below:

declare global {
   interface Selectors {
      foo: number
      bar: string
   }
}

However I would like to constrain the names the users can define in this interface to adhere to a certain naming pattern, specifically "select${Capitalize<string>}", meaning keys such as foo and bar would be unacceptable, rather selectFoo or selectBar would be suitable.

I was thinking of using the extends feature to extend a Record with such keys, but the problem is, just because type A extends type B, does not mean that the same constraints that are applied to B is applied to A (and it shouldn't).

I am unsure how to achieve this, as I need users to be able to define any keys they want, so long as they adhere to the pattern, yet this has to be a "declaration merging" type of interface.

I've found something similar in this post, however I am not sure how to use that, or even if it is the same kind of issue, i feel like it is not. Mine is more about naming, the type that is related to the property can be any type.

0

There are 0 answers