I'd like to create a common function which will take an object, then do some transformations and return new object with same keys and different values. I'm trying to make it strongly-typed, so everyone who uses it will have benefits of TS and non-existing keys should throw an error.
What I have for now:
const hash = {
first: 1,
second: 2,
third: 3,
}
type Mapper<T> = {
[key in keyof T]: number
}
type Result<T>= {
[key in keyof T]: () => number
}
const transform = <T>(mapper: Mapper<T>) => {
const result = {} as Result<T>
(Object.keys(mapper) as (keyof T)[]).map(key => {
result[key] = () => mapper[key]
})
return result
}
type Hash = typeof hash
const a = transform<Hash>(hash)
a.first()
// a.fifth() OK error
It works well, but I'm looking for solutions to solve this:
Remove type assertion
const result = {} as Result<T>
Remove type assertion
(Object.keys(mapper) as (keyof T)[])
(or useObject.entries
, but seems it also requires type assertion in this case)
Could I implement the same, but in more clean way in Typescript?