hegel icon indicating copy to clipboard operation
hegel copied to clipboard

Documentation: No mention of records in object

Open Raynos opened this issue 5 years ago • 4 comments

Does hegel support objects with arbitary keys ?

Like

const headers: { [key: string]: string } = req.headers
const headers: Record<string, string> = req.headers

An object with an arbitary key ( string or number I guess ... ) and a known value T, in the example above string but could by anything.

This was not found on https://hegel.js.org/docs/object-types

Raynos avatar May 22 '20 07:05 Raynos

Does hegel support objects with arbitary keys ? No. It supports it only inside the d.ts typing (via $Collection, but you can't create value for the type). Our position is: if you need key-value storage - use Map. But, we try to implement it inside inner typing to have the ability do like this

JSMonk avatar May 22 '20 12:05 JSMonk

Using {} as a dictionary instead of Map is important for using hegelJS on existing codebases.

I want to look at taking an existing JS library like https://github.com/Raynos/error/blob/master/index.js#L62-L76 and porting it to hegel.

Here is another example ( https://github.com/Raynos/fake-ses/blob/master/index.js#L112-L120 ).

I understand {} or Object.create(null) might not be as nice as Map but it should be supported.

Raynos avatar May 22 '20 12:05 Raynos

@JSMonk well objects with arbitrary keys sounds like a bad idea but Array is a object and a collection and we are okay with it

both flow and typescript supports this and it's common pattern in js so you really need to consider supporting it

thecotne avatar May 22 '20 15:05 thecotne

Okay, I will add the ability to use $Collection with objects soon. Thank you for the arguments and issue ^_^

JSMonk avatar May 22 '20 17:05 JSMonk