1

I am trying to use recursive generics in a way I can get a lot of help from editor.

Here is an example:

interface ServiceEndpointNode {
  self: string;
  context?: Record<string, ServiceEndpointNode>
}

const ServiceEndpoints: ServiceEndpointNode = {
  self: 'BASE_URL',
  context: {
    songs: {
      self: 'GET_SONGS',
      context: {
        getSong: {
          self: 'GET_SONG',
        },
        getContributors: {
          self: 'GET_CONTRIBUTORS',
        }
      }
    }
  }
}

This works properly and the structure is strict, but I don't got help from the editor. For example I want help like:

ServiceEndpoints.context.songs.context.getsong.self

But because I only told the typing that the context should be a string I don't really receiving help about the traversable object. I guess I need to be include some generics or something, but don't know how to achieve that. :(

So I want to have to maintain this strict structure, yet get help from the editor for all the possible routes, keys, etc.

  • Does [this approach](https://tsplay.dev/wE5J3w) meet your needs? The idea is that you want to *verify* that the variable `ServiceEndpoints` satisfies `ServiceEndpointNode` without *widening* it to that type. Let me know if I'm missing something. – jcalz Mar 09 '22 at 19:02
  • 1
    This is fab mate, this is exactly what I wanted! I mean its would be cooler without a function, but I guess what I want is quite special haha – Istvan Orban Mar 09 '22 at 21:10
  • Yeah, it's too bad there's no built-in `satisfies` operator as [this answer to the linked question](https://stackoverflow.com/a/70994696/2887218) mentions, so we have to implement it as a function. – jcalz Mar 09 '22 at 21:22
  • Makes sense... cheers man, you ROCK! – Istvan Orban Mar 09 '22 at 21:41

1 Answers1

0

Now you can use Optional chaining in TypeScript simplify as shown below. So that typescript compiler will expect NULL | underfined

    console.log(ServiceEndpoints.context?.songs.context?.getSong?.self);
    // OUTPUT: "GET_SONG" 
Jijo Alexander
  • 1,230
  • 8
  • 17