JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.1k stars 59 forks source link

Documentation: No mention of records in object #238

Open Raynos opened 4 years ago

Raynos commented 4 years ago

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

JSMonk commented 4 years ago

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

Raynos commented 4 years ago

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.

thecotne commented 4 years ago

@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

JSMonk commented 4 years ago

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