3 ms·
Calling "Object.freeze()" and then asking for the keys/values/entries of a frozen object should be able to return a properly-typed value. But it doesn't do thi
by gavinray 3y ago
Calling "Object.freeze()" and then asking for the keys/values/entries of a frozen object should be able to return a properly-typed value.
But it doesn't do this =(
- williamdclt 3y agoIt couldn’t, no: the object being frozen might contain more attributes than its type describes. Typescript can’t know these extra attributes.
- gavinray 3y agoAt the very least, it could return an intersection type that is equivalent to the known keys in the object so you get autocomplete, but allow other arbitrary properties of unknown type: type FrozenObject<T> = { readonly [K in keyof T]: T[K] } & { readonly [key: string]: unknown }; const x = { foo: 1, bar: "hello" } x["baz"] = "blah" const frozen = Object.freeze(x) as FrozenObject<typeof x> const baz = frozen.baz // No autocomplete for 'baz', but no error either