Here is simplified case what i am struggling.
const createObj = <T extends string>(keys: T[]) => {
return Object.fromEntries(
keys.map((key) => [key, 0])
);
};
const result = createObj(["hi","hello","gg"] as const)
I hoped the result type would be {hi: 0, hello: 0, gg: 0 }
but the result is any
;
CodePudding user response:
you can do it in this way :
function createObj<T extends string>(arr: T[]): Record<T, number> {
let result = {} as Record<T, number>;
arr.forEach((x, i) => {
result[x] = i;
})
return result
}
const object = createObj(["hi","hello","gg"])