What it does
Autocomplete suggests permission names as you type, so you don't have to remember every permission string. It catches typos and speeds up development.
When to use it
Autocomplete is helpful when:
- You have many permissions — dozens or hundreds
- You want to avoid typos — especially in production
- You want your editor's help — VS Code, WebStorm, etc.
Autocomplete requires TypeScript. It's built-in once you set up strict types.
How it works
- The generated permly.ts exports Permission type: automatically derived from your config
- Import the type: other files import it from
./permly.js - Use it in functions: permly validates against the type
- Your editor suggests names and highlights typos
Set up autocomplete
The generated permly.ts file automatically exports the Permission type. Just import it:
import { perms, Permission } from "./permly.js";
async function canUserAct(userId: number, action: Permission) {
return await perms.user(userId).can(action);
}
// ✓ Correct: "posts.edit" is a valid permission
const allowed = await canUserAct(42, "posts.edit");
// ✗ Wrong: "posts.edt" doesn't exist (typo)
// const allowed = await canUserAct(42, "posts.edt"); // Error!
Autocomplete in action
When you type, your editor suggests valid permission names:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
const allowed = await perms.user(42).can("posts.create");
// Type safety ensures only valid permissions are accepted:
// - posts.create ✓
// - posts.edit ✓
// - posts.delete ✓
console.log(allowed); // false (user 42 has no permissions)
Without autocomplete
If you pass a plain string, permly doesn't validate it:
import { perms } from "./permly.js";
// ✗ No validation—TypeScript allows any string, even with typos
const permission: string = "posts.edt"; // string type accepts any value
// @ts-expect-error: string is too general; use Permission type for safety
await perms.user(42).can(permission);
With the Permission type, typos are caught:
import { perms, type Permission } from "./permly.js";
// ✓ Validation—typo caught at compile time
// @ts-expect-error: "posts.edt" is not a valid permission (did you mean "posts.edit"?)
const permission: Permission = "posts.edt";
Use the Permission type across your codebase
Import the Permission type from your permly setup file in all modules that check permissions:
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: Permission type ensures only valid names are accepted
const allowed = await checkAccess(42, "posts.edit");
console.log(allowed); // false
Autocomplete with Express middleware
Create a typed middleware using the Permission type:
import express from "express";
import { perms, type Permission } from "./permly.js";
const app = express();
// Middleware that accepts only valid permission names
function requirePermission(permission: Permission) {
return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const userId = (req as any).user?.id;
if (!userId) return res.status(401).json({ error: "Not authenticated" });
const allowed = await perms.user(userId).can(permission);
if (!allowed) return res.status(403).json({ error: "Access denied" });
next();
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
};
}
app.post("/posts", requirePermission("posts.create"), (req, res) => {
res.json({ ok: true });
});
// ✓ "posts.create" is a valid permission
// app.delete("/posts/:id", requirePermission("posts.delete"), ...);
// ✗ "posts.edt" is not valid (typo caught at compile time)
// app.put("/posts/:id", requirePermission("posts.edt"), ...); // Error!
Common mistakes
Using string literals instead of the type — If you write "posts.edit" directly, there's no validation. Import and use the Permission type.
Forgetting to import the type — The Permission type is exported from the generated permly.ts file. Import it in all files that check permissions.
Importing from the wrong file — Always import Permission from ./permly.js, not from individual permission files.
Related pages
- Strict Mode — Type safety concepts
- Permissions — Permission fundamentals
- Plain JavaScript — Autocomplete requires TypeScript