Skip to content

index.Function.createSubjects

github-actions[bot] edited this page Nov 30, 2026 · 26 revisions

Function: createSubjects()

function createSubjects<TMap>(): <T>(map) => T;

Defined in: packages/graphql-casl/src/subjects.ts:82

Returns a helper that validates a subject-name const object against the keys of TMap.

Type Parameters

TMap

TMap extends Record<string, object>

The subject map, e.g. SubjectMap<Resolvers, ResolversTypes>.

Returns

A function that returns the object it is given.

<T>(map) => T

Deprecated

Use subjectsOf, which needs no object and stays in step with the schema on its own. This restates names TMap already carries, so every type added to the schema has to be listed here too — interfaces and unions included — before anything compiles again. It keeps working and is not scheduled for removal.

Remarks

The object's keys must exactly cover the derived domain type names — TypeScript errors if any are missing or misspelled. Values equal keys so each entry can be used directly in CASL calls.

Example

const Subject = createSubjects<AppSubjectMap>()({
  User: 'User', Note: 'Note', Org: 'Org', OrgMember: 'OrgMember',
} as const);

ability.can('read', Subject.User); // typed literal 'User', not plain string

API Reference

Clone this wiki locally