What it does
Strict mode is a TypeScript feature that prevents typos in permission names. Without strict mode, a typo like "posts.edt" silently returns false. With strict mode, typos are caught at compile time.
When to use it
Use strict mode if:
- You use TypeScript — it's zero setup once enabled
- You want type safety — catch permission name typos early
- You want autocomplete — your editor suggests permission names
Strict mode is recommended for all new projects.
How it works
- Define permissions in TypeScript:
as constto create a literal type - Use that type when checking: permly validates permission names
- TypeScript catches typos at compile time
Examples
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// ✓ This is correct
const canEdit: boolean = await perms.user(42).can("posts.edit");
// ✗ This is wrong (typo: edt instead of edit)
// TypeScript error: "posts.edt" is not a valid permission
// const canEdit = await perms.user(42).can("posts.edt");
Typed permissions from the setup file
The generated permly.ts file exports Permission and Role types for your app:
import { perms, setupPermissions, type Permission } from "./permly.js";
await setupPermissions();
async function checkAccess(userId: number, permission: Permission) {
return await perms.user(userId).can(permission);
}
// ✓ Correct: "posts.edit" is a valid permission
const allowed = await checkAccess(42, "posts.edit");
console.log(allowed); // false
Autocomplete
With typed permissions, your editor autocompletes permission names:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Your editor shows autocomplete for all valid permissions:
// - posts.create ✓
// - posts.edit ✓
// - posts.delete ✓
const allowed = await perms.user(42).can("posts.create");
console.log(allowed); // false
Common mistakes
Forgetting to import the type — The Permission type is exported from permly.ts. Import it in files that check permissions.
Using string literals without the type — If you pass a plain string without the Permission type, typos won't be caught. Always import and use the Permission type.
Mixing typed and untyped checks — Use the Permission type consistently across your codebase.
Related pages
- Permissions — Core concepts
- TypeScript Autocomplete — How autocomplete works
- Plain JavaScript — For non-TypeScript apps