0

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;

1 Answer 1

1

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"])

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.