sirisian / ecmascript-types

ECMAScript Optional Static Typing Proposal http://sirisian.github.io/ecmascript-types/
453 stars 4 forks source link

Object type keys and/or values #21

Closed sirisian closed 5 years ago

sirisian commented 7 years ago

Objects have generally been only strings as their keys. A typed object key would allow for more optimizations by the JIT closer inline with a map. That said this would drastically change how properties are treated. They've always been coerced to strings. By default that would still be true, but in a typed implementation they'd take on many properties of a Map. In the future if generics are added it's expected that a Map<Key, Value> change would be proposed. That said Map has different guarantees than objects so it's not a huge issue to consider such as property order.

The key types would be restricted to integers and enumeration types with the value types being unrestricted. A different syntax for key only and key and value would be supported. It's not possible to define only the value type.

The proposed syntax would be:

{
    0:string: 10,
    1:uint32: 34
}:uint32 /* key type */

{
    0: `foo`
}:(uint32, string) /* key, value type */

Example with ternary operator for reference of what that would look like in terms of readability:

let a:{}:uint32 = true ? {}:uint32 : {}:(uint32, string);
sirisian commented 7 years ago

This might conflict with the spirit of generics later:

const x:{}<uint32, string> = { 0: 'a', 1: 'b' };
Map<Key, Value>
Set<Value>

Clearly are in generic syntax which will be implemented way later. It makes sense that an Object generic syntax fits more than trying to tie in a tuple type thing into the object syntax. There might be a far better use case for colon syntax down the line with objects that I'm not even seeing.

I'll keep this open if anyone has thoughts or future considerations, but I won't be proposing any object syntax changes other than the ones presented already.