P
P
partyzanx2022-01-08 10:43:09
typescript
partyzanx, 2022-01-08 10:43:09

How to assign a type to a cloned object whose values ​​have been changed recursively?

Hello. I make a deep copy of 1 object into a new object, changing the key values ​​from numbers to strings. How to set the type for a new object, correcting all values ​​to type "string" recursively in depth? Here is the file https://codesandbox.io/s/epic-platform-mezbe

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2022-01-08
@partyzanx

You can add this type:

type RulersMap<R extends Record<string, unknown>> = {
  [K in keyof R]: R[K] extends number
    ? `${R[K]}px`
    : R[K] extends Record<string, unknown>
      ? RulersMap<R[K]>
      : R[K];
};
and cast the result to the type RulersMap<typeof rulers>
The problem is that map-obj types are not very well written for the deep version, although you can write your own if you wish.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question