英文:
Typescript 2 Generic types dependent on each other
问题
我有两种类型的配置,一种用于身份验证,另一种用于一般配置,不包含任何秘密变量。我曾经有两个函数,但现在我尝试只使用一个函数,但我无法实现这段代码,出现了错误,我找不到我做错了什么。
interface Config {
config: string;
config_example: string;
}
interface Auth {
auth: string;
auth_example: string;
}
type ConfigType = "auth" | "config";
type GetKeyType<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 GetKeyType<C>>(configType: C, key: K): GetKeyType<C>[K] {
switch (configType) {
case "auth":
return auth[key];
case "config":
return config[key];
default:
throw new Error();
}
}
这是你的代码,我已经将HTML实体转义字符(如"
)更改为双引号。你可以在 TypeScript Playground 上使用这个代码链接进行测试:[此处](https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgQexDYBzZBvAKGWNQy2wC5kBnMKUbAbiJPUxwH0IAPOAWwAOAGwhVa9EEwIBfZAQKhIsRCjgBXMAAt8LYuq1i6DZiWT7NXXoJGGJU6fLABPASjbkAKi5QBeZACJzf2QAHwD3HH9mZ1dkbAgwL1cAHgBhZB5IEAATalJ2bCSIAD5kP3TM
iBy8wI1NYIB+ZABrCCc0GDM65CoK7izc8LJI5CbW9s6I3CoQCAA3aGYCNlougzXtP0JTcyoAcj2AGl0Ny35hUWQDmXkVsHzyKimynVMp-aOTqbPrS+uHAgwNQgBBgYAYOIJVLDbDJE59AZ5KZFY6mADSGX6VUG4w6kMS3jSxQIxQAFO9kKlDi02lQ0QBKKjxAkpVLFADaaIAuq8SNQAO7AMAIbTkmH03mmYgIODUFC1LT+CgnKUkKAJNRQEAbdnjLkmVWoWXyqZKlWG9VgTXaqa6tr683EbIQeBqIRgZWGqVaKBofnIWb+gCiUF9UFJ9INxAcsiAA)。
如果你在IDE中添加//@ts-ignore来忽略错误,IDE会正确识别类型,但我理解你不想使用//@ts-ignore。这个代码看起来没有什么问题,如果你有特定的错误或问题,请提供更多细节,我将尽力帮助你找到解决方案。
英文:
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?
答案1
得分: 1
请查看Wishlist: support for correlated union types #30581
const allConfigs = {
"auth": auth,
"config": config
}
function getConfig<
CONFIG_NAME extends keyof typeof allConfigs,
CONFIG_KEY extends keyof typeof allConfigs[CONFIG_NAME]
>(config: CONFIG_NAME, key: CONFIG_KEY): typeof allConfigs[config][key];
英文:
See Wishlist: support for correlated union types #30581
const allConfigs = {
"auth": auth,
"config": config
}
function getConfig<
CONFIG_NAME extends keyof typeof allConfigs,
CONFIG_KEY extends keyof typeof allConfigs[CONFIG_NAME]
>(config: CONFIG_NAME, key: CONFIG_KEY): typeof allConfigs[CONFIG_NAME][CONFIG_KEY] {
return allConfigs[config][key];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论