TypeDrop

A new TypeScript challenge every day. Sharpen your types.

TypeDrop delivers a fresh TypeScript challenge every day, generated by AI. Pick a challenge, open it in StackBlitz (preferred) or CodeSandbox (or clone it locally), and make the tests pass. No accounts, no setup — just you and the type system.

Learn more on GitHub →

2026-12-04 Hard

Typed Permission System: Role Hierarchies, Policy Evaluation & Access Audit

You're building the authorization core for a multi-tenant SaaS platform. Every API action must be checked against a policy engine that resolves role hierarchies, evaluates attribute-based conditions, and records a fully-typed access decision in an audit trail — the compiler must enforce every role shape, policy variant, condition predicate, and decision outcome.

Goals

  • Define all branded ID types, discriminated union types (Role, Condition, Policy, AccessDecision), and the Frozen<T> mapped-type utility with zero use of `any`.
  • Implement collectAllowedActions with recursive inheritance resolution and a cycle-guard Set, and evaluateCondition with exhaustive narrowing over every Condition variant.
  • Implement evaluatePolicy returning 'allow' | 'deny' | 'inapplicable' by checking action membership and evaluating all attached conditions.
  • Implement evaluateAccess using the full deny-overrides-allow algorithm (explicit deny → granted → no_matching_allow) and auditAccess to append Frozen decisions to the log.
challenge.ts
// Core types and main function signature preview

declare const __brand: unique symbol;
type Brand<T, B> = T & { readonly [__brand]: B };

type PolicyId   = Brand<string, "PolicyId">;
type TenantId   = Brand<string, "TenantId">;
type UserId     = Brand<string, "UserId">;

type Action = "read" | "write" | "delete" | "admin" | "share";

type Role =
  | { kind: "builtin"; name: "viewer" | "editor" | "owner" }
  | { kind: "custom";  name: string; inherits: ReadonlyArray<Role> }
  | { kind: "service"; serviceId: string; scopes: ReadonlyArray<Action> };

type AccessDecision =
  | { verdict: "granted"; requestId: string; grantedBy: PolicyId }
  | { verdict: "denied";  requestId: string;
      reason: "explicit_deny" | "no_matching_allow" | "condition_failed";
      failedPolicyId: PolicyId | null };

type Frozen<T> = { readonly [K in keyof T]: T[K] };

// Main evaluation entry point
declare function evaluateAccess(request: AccessRequest): AccessDecision;

// Audit wrapper — log entries are frozen once appended
declare function auditAccess(
  request: AccessRequest,
  log: Frozen<AccessDecision>[]
): AccessDecision;
Hints (click to reveal)

Hints

  • For Frozen<T>, a mapped type `{ readonly [K in keyof T]: T[K] }` makes every property readonly — apply it to AccessDecision before pushing to the log.
  • Use a `Set<Role>` passed by reference through recursive calls to collectAllowedActions and resolveRolePolicies to break cycles — check `visited.has(role)` before recursing.
  • In evaluateAccess, collect deny results first with Array.filter narrowed to the 'deny' branch; if any exist, short-circuit — only then look for the first 'allow'.

Or clone locally

git clone -b challenge/2026-12-04 https://github.com/niltonheck/typedrop.git