Challenging recursive TS type
Had an annoying time with this one today at work. See this playground for repro: https://t.ly/C--h
One of my APIs has a quirk that the JSON it provides gives every object back wrapped in a surrounding
{ map: <obj> }
. I can write a simple recursive function to mapWrap some data, or to unMapWrap it when I receive it. I can also define a generic type that expresses the relationship between a MapWrapped<T>
and the unwrapped type. But I don't know how to make TS understand that the mapWrap<T>
function produces the MapWrapped<T>
type without casting to any
multiple times.TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
0 Replies