0

I am trying to write a function "getSetting(settingPath: string) => something", where typescript gives me the correct return type based on the given parameter. Sadly this isn't just simple object access in this function as there are default fallbacks and right checks happening inside.

I have an interface that describes how my settings look like, this interface is non-circular, but nested, e.g.

interface Settings{
  title: string;
  author: string;
  daysUp: number:
  meta: {
    key1: string;
    key2: string:
    special: {
       key3: number;
       key4: boolean;
    }
  }
}

so nested, but non-recursive and non-circular.

I also have a method of finding all possible dot-notation strings and filter the values by type and so on (most comes in an adapted for from here: Typescript: deep keyof of a nested object)

type SettingsPaths = LeavesArray<PublishedSettings>
// SettingsPaths = ["title"] | ... ["meta", "key1"] | ...

I also have already defined a Type like the following where S is the Interface defined above and P is an Array of chained keys leading to the sub-interface

type GetSettingFunction<S, P> = (settings: S, settingPath: JoinArray<P, '.'>) => TypeOfSubinterface<P>

what I now need to do is to combine those two things to get a combined type with all valid combination of values for settingPath and the return value, so to say the following:

type GetSettingValidCalls = GetSettingFunction<S, "title"> | ... | GetSettingFunction<S, ["meta", "key1"]> | ...

however, without needing to type each and every of these types, but generated from the SettingsPaths type above. any ideas?

EDIT: A complete example can be found at Playground

Tobi
  • 1,175
  • 1
  • 19
  • 44
  • 1
    Could you please provide what you have so far in TS playground ? U did not provide LeavesArray – captain-yossarian from Ukraine Mar 29 '21 at 20:04
  • Please consider modifying the code in this question so as to constitute a [mcve] which, when dropped into a standalone IDE like [The TypeScript Playground](https://tsplay.dev/Nl0lGN), clearly demonstrates the issue you are facing. This will allow those who want to help you to immediately get to work solving the problem without first needing to re-create it. And it will make it so that any answer you get is testable against a well-defined use case. – jcalz Mar 29 '21 at 20:08

0 Answers0