You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Come up with some syntax for classes to implement a Record pattern / index signature, while having members/methods that do not conform to its constraints.
I'm not sure how this would be implemented. Maybe what's needed is a "complement" type, i.e. a formal internal representation of Exclude<string, "a" | "b"> that doesn't just collapse down to string?
Use Cases
There seems to be wide consensus that implementing the Record pattern directly on classes is A Bad Idea, but that doesn't change the fact that legacy codebases exist that use this pattern. I came here because I'm trying to describe the shape of this PropertyBag class but there are a bunch of StackOverflow questions (and a few issues here) where people are asking for the same thing
I've done pretty extensive research and have found a number of partial workarounds, but none that fully describe a type that:
a) is new-able,
b) types its unknown properties strongly, and
c) types is known properties correctly
The closest I've gotten is this union type where you declare a base class and an interface with an index signature, then declare a union type that mashes them together, but as the Playground example shows, that union isn't new-able.
(Moving the bag-of-values down to a member property, would be a good idea, but I'm trying to describe the shape of code I don't own.)
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
Thanks @j-oliveras , I didn't have the right terminology for "negated" -- "complement" turned up in a lot of set-theory articles that also use TS terms "union" and "intersection".
But! Also! Before anybody mashes that "Close as Duplicate" button, I'd like to point out that I'm trying to solve a use case -- strongly typed indexing on a "normal" class -- not to advocate one specific solution. While negated types would almost certainly solve this, I'd like to leave this as a separate issue to see if any other promising solutions can be proposed.
Search Terms
index signature class extend record
Suggestion
Come up with some syntax for classes to implement a
Record
pattern / index signature, while having members/methods that do not conform to its constraints.I'm not sure how this would be implemented. Maybe what's needed is a "complement" type, i.e. a formal internal representation of
Exclude<string, "a" | "b">
that doesn't just collapse down tostring
?Use Cases
There seems to be wide consensus that implementing the
Record
pattern directly on classes is A Bad Idea, but that doesn't change the fact that legacy codebases exist that use this pattern. I came here because I'm trying to describe the shape of this PropertyBag class but there are a bunch of StackOverflow questions (and a few issues here) where people are asking for the same thingDetails
I've done pretty extensive research and have found a number of partial workarounds, but none that fully describe a type that:
a) is
new
-able,b) types its unknown properties strongly, and
c) types is known properties correctly
The closest I've gotten is this union type where you declare a base class and an interface with an index signature, then declare a union type that mashes them together, but as the Playground example shows, that union isn't
new
-able.(Moving the bag-of-values down to a member property, would be a good idea, but I'm trying to describe the shape of code I don't own.)
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: