Repository navigation
index.Function.subjectsOf
function subjectsOf<TMap>(): Subjects<TMap>;Defined in: packages/graphql-casl/src/subjects.ts:48
Returns a Subjects namespace for TMap — autocompleted, typo-proof
subject names, with nothing to declare.
TMap extends Record<string, object>
The subject map, e.g. SubjectMap<Resolvers, ResolversTypes>.
Subjects<TMap>
A namespace mapping each subject name to itself.
The names live entirely in TMap, so there is no list to keep in step with
the schema: adding a type to the schema makes Subject.NewType available on
the next codegen run, and removing one turns every reference into a compile
error. Bound to a map exactly like createTyped.
Note that Subject is a convenience, not a requirement — every API that takes
a subject name accepts the bare string literal and checks it just as strictly,
so can('read', 'User') is equivalent to can('read', Subject.User). Reach
for this when you want the autocomplete and the rename/find-references anchor.
The returned object is a Proxy that answers every property with its own
name, so it cannot be enumerated: Object.keys(Subject) is [], spreading it
yields {}, and JSON.stringify gives "{}". Property access is the only
supported operation. Symbol properties read as undefined so the object still
behaves normally when inspected, awaited, or coerced.
const Subject = subjectsOf<AppSubjectMap>();
ability.can('read', Subject.User); // typed literal 'User', not plain string
Subject.Uesr; // compile error: no such property