最初的回答:
我有这个类型:
换句话说,如何将类型 PermissionType 编译为JavaScript数组,以便我可以轻松执行 indexOf(userInput)> -1 。最初的回答:
export type PermissionType = 'creator' | 'editor' | 'viewer'
在运行时,如何检查变量userInput是否实际上是上述类型之一
提示:该问题涉及it技术。
原始答案翻译成中文为"最初的回答"。
let userInput = 'foo' //
isOfTypePermission(userInput) // should return false or throw an error
let userInput2 = 'creator'
isOfTypePermission(userInput2) // should return true
换句话说,如何将类型 PermissionType 编译为JavaScript数组,以便我可以轻松执行 indexOf(userInput)> -1 。最初的回答:
switch
语句。 - grooveplex