gpt4 book ai didi

javascript - 使用对象键作为另一个对象的键

转载 作者:行者123 更新时间:2023-12-05 01:54:53 24 4
gpt4 key购买 nike

我想在 TypeScript 中创建一个类型并使用另一个对象的值作为它的键

const routes = {
home: "HOME",
profile: "PROFILE"
}

export type NavigatorParamList = {
[routes.home]: undefined;
[routes.profile]: undefined;
};

我知道这段代码不起作用!知道怎么做吗?

最佳答案

您的对象的属性是可变字符串,TypeScript 不允许您将它们用作类型的计算属性,除非您使用 as const 将它们设为只读文字。

const routes = {
home: "HOME",
profile: "PROFILE"
} as const;

关于javascript - 使用对象键作为另一个对象的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70483425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com