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