【发布时间】:2020-10-07 07:41:04
【问题描述】:
假设我有一个这样的函数,但枚举参数有更多可能的值:
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 可以是callAPI(API.user.comments) 而不是callAPI(API.userComments)。
我尝试了以下尝试,但似乎没有一个被接受为打字稿的有效语法。
尝试 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
}
【问题讨论】:
标签: typescript enums