I have the following initial object
const initialValues = { name: 'Jon', email: '[email protected]' }
I would like to create an identical object except where all the values are boolean
and default to false
. Like so
const expected = { name: false, email: false }
I created the following function which does what I want
const expected = cloneWithDefaults<typeof initialValues>(initialValues)
function cloneWithDefaults<T>(values: T) {
type U = { [K in keyof T]: boolean }
const keys = Object.keys(values) as Array<keyof T>
const partial: Partial<U> = keys.reduce<Partial<U>>((acc, cur) => {
acc[cur] = false
return acc
}, {})
const final = partial as U
return final
}
I also created a second version which does not use reduce
function createCloneWithDefaultsV2<T extends { [key: string]: unknown }>(values: T) {
type U = { [K in keyof T]: boolean }
const keys = Object.keys(values) as Array<keyof U>
const partial: Partial<U> = {}
for (let k in keys) {
partial[k] = false
}
const final = partial as U
return final
}
I wonder if there is a better / more succinct way of doing this. In particular I would like to get rid of the two uses of as
if possible.
In the v2 I wonder if there is any preference for unknown
as opposed to any
.
Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.
No, JavaScript objects cannot have duplicate keys. The keys must all be unique.
keyof is a keyword in TypeScript which is used to extract the key type from an object type.
Here's how I would write it:
function cloneWithDefaults<T>(values: T) {
return <{[key in keyof T]: boolean}> Object.entries(values).reduce((p, [k, v]) => Object.assign(p, { [k]: false }), {});
}
const initialValues = { name: 'Jon', email: '[email protected]' };
const expected = cloneWithDefaults(initialValues);
Due to the reduce() call, I don't see an option without a cast. (Either from partial or from any.) Note that Object.entries() requires downlevelIteration
ts compiler option to be true (https://www.typescriptlang.org/docs/handbook/compiler-options.html).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With