I have two types of config one for auth and one for general config which doesn't have any secret variables I used to have two functions but now I'm trying to have only one function but I wasn't able to implement this code with errors, I couldn't find what I'm doing wrong
interface config {
config: string;
config_example: string;
}
interface auth {
auth: string;
auth_example: string;
}
type configType = "auth" | "config";
type getType<C extends configType> = C extends "auth" ? keyof auth : C extends "config" ? keyof config : never;
const auth: auth = {
auth: '',
auth_example: ''
}
const config: config = {
config: '',
config_example: ''
}
function getConfig<
C extends configType,
K extends keyof getType<C>
>(config: C, key: K): getType<C>[K] {
switch (config) {
case "auth":
return auth[key];
case "config":
return config[key];
default:
throw new Error();
}
}
here's the typescipt playground code of this
If I put //@ts-ignore to the errors, the IDE correctly determines the types and everything but I don't want to implement this with //@ts-ignore I don't even know if this is possible to do that but any ideas?