mswjs / data

Data modeling and relation library for testing JavaScript applications.
https://npm.im/@mswjs/data
MIT License
823 stars 52 forks source link

Definition for Objects with dynamic keys #252

Open brogine opened 2 years ago

brogine commented 2 years ago

Hi everyone!

First of all, thanks a lot for this project. It was the missing feature that MSW was lacking. So far I've found it easy to use and extremely simple to setup.

Let me describe the issue that I'm facing at the moment. I was not able to find a way to describe an estructure with dynamic object keys. Let's say I've this in TS:

{
  models: {
    [key: string]: {
      attr: number
      attr2: string
    }
  }
}

Is there a way to do it? I tried adding Object as a type, but when I create elements, they are created as an empty object. Please, let me know if this is a missing feature. I'll try to create a PR for it.

kettanaito commented 2 years ago

Hey, @brogine. Thanks for your kind words!

Dynamic object property support is not currently present. Moreover, I can't say for certain how it would align with the current API. If you have some proposals on this, I'd be glad to support you in making this happen. But if this support implies rewriting the library, I'd recommend against it, as I'm in a middle of such a rewrite already (that's been going on for quite some time but it's destined to land eventually).

manzaloros commented 1 year ago

I have this same issue.

Basically I have a field on a model that is an array of objects that can be one of 2 types.

But there is no way in mswjs/data to model an object that can have variable properties.

timtos commented 8 months ago

Any updates on this? I have a Record<string, string> type that wants to be mocked. :)