如何在TypeScript中嵌套枚举?
假设我有一个这样的函数,但枚举参数有更多可能的值:如何在TypeScript中嵌套枚举?,typescript,enums,Typescript,Enums,假设我有一个这样的函数,但枚举参数有更多可能的值: enum API{ userDetails = "/api/user/details", userPosts = "/api/user/posts", userComments = "/api/user/comments", postDetails = "/api/post/details", postComments = &
enum API{
userDetails = "/api/user/details",
userPosts = "/api/user/posts",
userComments = "/api/user/comments",
postDetails = "/api/post/details",
postComments = "/api/post/comments"
//...
};
function callAPI(endpoint: API/*, some more params*/){
// some code to deal with the specified endpoint...
}
callAPI(API.userComments);
上面的代码工作得很好,但我需要将枚举的许多值分组到一个有组织的层次结构中,以便使事情更有组织。。。例如,如果调用API的语法可以是callAPIAPI.user.comments,而不是callAPIAPI.userComments,那就更好了
我尝试了以下尝试,但似乎没有一个被接受为typescript的有效语法
尝试1
enum API{
user = {
details : "/api/user/details",
userPorts : "/api/user/posts",
userComments : "/api/user/comments"
}
post = {
postDetails : "/api/post/details",
postComments : "/api/post/comments"
}
}
尝试2
enum user {
details = "/api/user/details",
userPorts = "/api/user/posts",
userComments = "/api/user/comments"
}
enum post {
postDetails = "/api/post/details",
postComments = "/api/post/comments"
}
enum API{
user,
post
}
尝试3
enum user {
details = "/api/user/details",
userPorts = "/api/user/posts",
userComments = "/api/user/comments"
}
enum post {
postDetails = "/api/post/details",
postComments = "/api/post/comments"
}
enum API{
user = user,
post = post
}
尝试4
enum user {
details = "/api/user/details",
userPorts = "/api/user/posts",
userComments = "/api/user/comments"
}
enum post {
postDetails = "/api/post/details",
postComments = "/api/post/comments"
}
enum API{
user:user
post:post
}
我的理解是使用枚举是不可能的-枚举成员的右侧可以是nothing、number、string或其他枚举值。 有关更多详细信息,请参阅 但是,您可以轻松地使用普通对象实现您想要的 常量API={ 用户:{ 详细信息:/api/用户/详细信息, posts:/api/user/posts, }, // ... }; 如果您担心它可能会被修改,您可以使用 如果您需要通过参数将整个API对象传递给函数,那么您也需要为它定义一个接口,否则自动完成和检查应该可以正常工作 ----编辑 添加类型以防您发现它们有用: 导出类型ApiRoutes={[key in TRoute]:string}; 导出接口MyApi{ 用户:apirouts ;; 邮政:apirouts ;; }
我的理解是使用枚举是不可能的-枚举成员的右侧可以是nothing、number、string或其他枚举值。 有关更多详细信息,请参阅 但是,您可以轻松地使用普通对象实现您想要的 常量API={ 用户:{ 详细信息:/api/用户/详细信息, posts:/api/user/posts, }, // ... }; 如果您担心它可能会被修改,您可以使用 如果您需要通过参数将整个API对象传递给函数,那么您也需要为它定义一个接口,否则自动完成和检查应该可以正常工作 ----编辑 添加类型以防您发现它们有用: 导出类型ApiRoutes={[key in TRoute]:string}; 导出接口MyApi{ 用户:apirouts ;; 邮政:apirouts ;; }
您可以将其包装在: 名称空间API{ 导出枚举用户{ 详细信息=/api/user/details, posts=/api/user/posts, comments=/api/user/comments, } 导出枚举后{ 详细信息=/api/post/details, comments=/api/post/comments, } } console.logAPI.User.comments console.logAPI.Post.details
您可以将其包装在: 名称空间API{ 导出枚举用户{ 详细信息=/api/user/details, posts=/api/user/posts, comments=/api/user/comments, } 导出枚举后{ 详细信息=/api/post/details, comments=/api/post/comments, } } console.logAPI.User.comments console.logAPI.Post.details
您还可以在对象末尾添加as const,使其仅可读。这不会阻止运行时修改,但类型应该阻止编译修改itI的尝试。我想我将使用它。尽管我更愿意使用API作为类型来实现类型验证。这有点粗糙,但您可以使用这些类型:如前所述,在对象初始化后使用as const。然后,为了获得您的类型,首先创建一个通用用例类型ValueOf=T[keyof T];它提供了对象值的并集。然后键入APIRoute=ValueOf,其中提供了值的值,即字符串routes。只要深度保持不变,这应该可以工作,idk如何为动态深度执行此操作您还可以在对象末尾添加常量,使其仅可读。这不会阻止运行时修改,但类型应该阻止编译修改itI的尝试。我想我将使用它。尽管我更愿意使用API作为类型来实现类型验证。这有点粗糙,但您可以使用这些类型:如前所述,在对象初始化后使用as const。然后,为了获得您的类型,首先创建一个通用用例类型ValueOf=T[keyof T];它提供了对象值的并集。然后键入APIRoute=ValueOf,其中提供了值的值,即字符串routes。只要深度保持不变,这应该可以工作,idk如何为动态深度执行此操作