Skip to content

Interface: RbacTypeConfig ​

Defined in: packages/core/src/types.ts:70

Type-level configuration of an Rbac instance — the equivalent of spatie's enums.

ts
const rbac = createRbac<{ permissions: 'edit articles' | 'delete articles'; roles: 'writer' }>({ adapter })
await rbac.for(user).can('edit articles') // ok
await rbac.for(user).can('fly')           // type error

Both keys are optional; a missing key accepts any string. TS string enums work too.

To type the names of each guard separately, use guards (and defaultGuard when it is not web) instead:

ts
type AppRbac = {
  guards: {
    web: { roles: 'admin' | 'editor'; permissions: 'articles.edit' | 'api-keys.manage' }
    api: { roles: 'read-only' | 'full-access'; permissions: 'orders.read' | 'orders.write' }
  }
  defaultGuard: 'web'
}

Names then follow the guard in use: rbac.for(user) takes the default guard's names, rbac.for(key, { guard: 'api' }) the api names. With guards, the top-level roles / permissions are not used.

Properties ​

PropertyTypeDescriptionDefined in
defaultGuard?stringWith guards: the guard used when none is given. Must match createRbac({ defaultGuard }). Default: 'web'.packages/core/src/types.ts:80
guards?objectNames per guard: { web: { roles: ...; permissions: ... }; api: { ... } }, each shaped like RbacGuardTypes. Only those guards are accepted as a guard option. Default: none (one set of names for every guard, from roles / permissions).packages/core/src/types.ts:78
permissions?string-packages/core/src/types.ts:71
roles?string-packages/core/src/types.ts:72

Released under the MIT License. Inspired by spatie/laravel-permission.