打字稿:枚举扩展(命名空间)导致错误...(中间值)是不可能的
Update:
我想知道这是命名空间中功能的出口。如果我删除导出
,则代码有效。
我正在尝试在我的应用程序上设置一些权限控制,各种资产都使用此权限。
我还想要一种简单的蜘蛛许可方法,以便将主持人自动获得JRMOD的许可,而JRMOD拥有Babymod的权限。
使用Member.Haspermission(Memberpermission.jrmod)将授予JRMOD和主持人的权限,以及Admin,因为HASPERMISENT
还检查以查看成员是否具有 Tech> Tech 和管理员
权限。
行中获得错误
(intermediate value)(intermediate value)(intermediate value) is not iterable
此代码有效 ,如果我删除了下面的名称空间位,否则我会在标记的 类似的事情,我
Found non-callable @@iterator
在这里得到了代码
enum MemberPermission {
tech = 'tech', // Supreme access
admin = 'admin', // Has most access
moderator = 'moderator',
jrMod = 'jrMod',
babyMod = 'babyMod'
};
namespace MemberPermission {
export function fromString(perm: string): MemberPermission {
return (MemberPermission as any)[perm];
}
}
type NonPrivilegedPermissions = Exclude<MemberPermission, "admin" | "tech">
let MemberPermissionMapSetup: {
[key in NonPrivilegedPermissions]?: NonPrivilegedPermissions[]
} = {
moderator: [MemberPermission.jrMod],
jrMod: [MemberPermission.babyMod]
};
let MemberPermissionMap: typeof MemberPermissionMapSetup = {};
function spiderPermissions(mp: keyof typeof MemberPermissionMapSetup): NonPrivilegedPermissions[] {
if (MemberPermissionMapSetup[mp] != undefined) {
// V V V V THIS LINE
MemberPermissionMap[mp] = [...MemberPermissionMap[mp] ?? [], ...MemberPermissionMapSetup[mp]!];
MemberPermissionMapSetup[mp]!.forEach((child, v) => {
MemberPermissionMap[mp]!.push(...spiderPermissions(child as NonPrivilegedPermissions))
})
}
MemberPermissionMap[mp] = Array.from(new Set(MemberPermissionMap[mp]));
return MemberPermissionMap[mp] ?? [];
}
for (let p in MemberPermission) {
spiderPermissions(p as NonPrivilegedPermissions);
}
console.log(MemberPermissionMap);
Update: Playground
I figured out that it's specifically the export of the function inside the namespace. If I remove export
, the code works.
I'm trying to setup some permissions control on my app, various assets use this.
I also wanted an easy way to spider permissions, so that moderator was automatically granted the permissions of jrMod, and jrMod had the authority of babyMod.
Using Member.hasPermission(MemberPermission.jrMod) would grant authority to jrMod, and moderator, as well as admin because hasPermission
also checks to see if the member has tech
and admin
permission.
This code works, if I remove the namespace bit below, otherwise I get an error at the marked line
(intermediate value)(intermediate value)(intermediate value) is not iterable
Or, if I use push
to do a similar thing, I get
Found non-callable @@iterator
Here's the code
enum MemberPermission {
tech = 'tech', // Supreme access
admin = 'admin', // Has most access
moderator = 'moderator',
jrMod = 'jrMod',
babyMod = 'babyMod'
};
namespace MemberPermission {
export function fromString(perm: string): MemberPermission {
return (MemberPermission as any)[perm];
}
}
type NonPrivilegedPermissions = Exclude<MemberPermission, "admin" | "tech">
let MemberPermissionMapSetup: {
[key in NonPrivilegedPermissions]?: NonPrivilegedPermissions[]
} = {
moderator: [MemberPermission.jrMod],
jrMod: [MemberPermission.babyMod]
};
let MemberPermissionMap: typeof MemberPermissionMapSetup = {};
function spiderPermissions(mp: keyof typeof MemberPermissionMapSetup): NonPrivilegedPermissions[] {
if (MemberPermissionMapSetup[mp] != undefined) {
// V V V V THIS LINE
MemberPermissionMap[mp] = [...MemberPermissionMap[mp] ?? [], ...MemberPermissionMapSetup[mp]!];
MemberPermissionMapSetup[mp]!.forEach((child, v) => {
MemberPermissionMap[mp]!.push(...spiderPermissions(child as NonPrivilegedPermissions))
})
}
MemberPermissionMap[mp] = Array.from(new Set(MemberPermissionMap[mp]));
return MemberPermissionMap[mp] ?? [];
}
for (let p in MemberPermission) {
spiderPermissions(p as NonPrivilegedPermissions);
}
console.log(MemberPermissionMap);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:一个分辨率
,因此整个麻烦似乎是会员Persermission上我的名称空间“扩展方法”之一被命名为
toString
。仅重命名此功能解决了问题。除了尝试捕捉,无法防止错误。
在成员Persermission的成员上循环,
HasownProperty
,String
for.constructor。 name
,.call
返回的所有成员返回的未定义,以使方法没有被蜘蛛,我将其重命名为
__
,以便我可以检查循环中的名称。如果我再次做类似的事情,我将不会使用此结构,以便这些成员Persermission方法不是会员Persermission的一部分,但是在这里很好。更改它需要的时间超过价值,因为仅在服务器启动时调用所有这些代码。
旧答案:
目前有效的解决方案是将
namepace
声明移至呼叫之后spiderpermissions()
,这感觉很奇怪,一个可能并不总是在不同的不同情况。如果Spiderpermission需要调用其中一种方法怎么办?我不知道。另一种解决方案将枚举方法(名称空间函数)删除到正常函数。
如果有人有一个,我仍然想要一个真正解决问题的解决方案。
Update: A resolution
So the entire trouble seems to be that one of my namespace "extension methods" on MemberPermission was named
toString
. Merely renaming this function fixed the issue.Short of a try-catch, there's no way to prevent the error.
Looping over the members of MemberPermission,
hasOwnProperty
,typeof string
String
for.constructor.name
,.call
returned undefined for all membersSo that methods aren't being spidered, I renamed them to be prefixed with
__
, so that I can check the name in the loop.If I did something like this again, I wouldn't use this structure so that these MemberPermission methods weren't part of MemberPermission, but it's fine here. Changing it would require more time than it's worth as all this code is only invoked at server start.
Old answer:
A working solution for now was to move the
namespace
declaration to after the call tospiderPermissions()
, which feels kind of weird, an may not always work in different situations. What if spiderPermission needs to call one of those methods? I don't know.Another solution removing the enum's methods (namespace functions) to just normal functions.
If anyone has one, I'd still like a solution that actually addresses the problem.