Our configurations require server-side data in this format
const permissionOptions = [
'read:workspace',
'create:bot',
'create:invitation',
'delete:bot',
] as const
The objective is to simplify this data into a Result
object like the following
type Result = {
read: {
workspace: Boolean
},
create: {
bot: Boolean,
invitation: Boolean,
},
delete: {
bot: Boolean
}
}
This is our progress so far (playground)
type PermissionString = typeof permissionOptions[number]
type UnionActions<T extends string> = T extends `${infer Action}:${string}` ? Action : never
type UnionKeys<A extends PossibleActions, T extends string> = T extends `${A}:${infer Key}` ? Key : never
type PossibleActions = UnionActions<PermissionString>
type PossibleKeys = UnionKeys<PossibleActions, PermissionString>
type Result = Record<PossibleActions, Record<PossibleKeys, boolean>>
function test(r: Result): void {
r.read.bot // read should only have `workspace`
r.create.workspace // create should only have 'bot' | 'invitation'
}