It would be nice to be able to declare an object or array as frozen at declaration time.
I have no strong opinions on actual syntax, so I'll use one I've seen elsewhere on this board: {# #}
and [# #]
I think this will be helpful on a number of levels:
- I assume this would help JS engines optimize storage of an object if they know it is read-only from creation, rather than relying on Object.freeze after the unfrozen object has been created.
- It would make declaring recursively frozen objects/arrays more readable and easier to declare:
const foo = {# contents: {# one: 1, two: 2 #}, values: [# 1,2,3 #] #}
as opposed to
const foo = Object.freeze({ contents: Object.freeze({ one: 1, two: 2 }), values: Object.freeze([ 1,2,3 ]) }
- This could propagate up to other languages like typescript, which could infer
readonly
on the declared object properties directly, rather than having to depend on wrappers like Readonly<>