There is a code error here, how to solve it。

Viewed 32

There is a code error here, how to solve it。

const api = { a: 1, b: { c: 2, d: 3, e: { f: 4, g: 5 } } };
interface apiProps {
  [name: string]: number | apiProps;
}
function format<T extends apiProps, S extends keyof T, P extends string>(obj: T, str: P): Record<`${S}${P}`, number> {
  const result = {} as Record<`${S}${P}`, number>;
  for (const key in obj) {
    const item = obj[key];
    if (typeof item === "number") {
      result[`${str}${key}`] = item;
    } else {
      const children = format(item as apiProps, (str + key) as P);
      Object.assign(result, children);
    }
  }
  return result;
}

const newApi = format(api, "");
console.log(newApi);

I want to be able to use it with hints

0 Answers
Related