fix: access control indexing type (#8155)

Co-authored-by: Alex Yang <himself65@outlook.com>
This commit is contained in:
YevheniiKotyrlo
2026-03-03 06:19:15 +02:00
committed by GitHub
parent 29b7567c2b
commit 47bba48f28
5 changed files with 10 additions and 8 deletions

View File

@@ -1,6 +1,8 @@
import type { LiteralString } from "@better-auth/core";
import type { AuthorizeResponse, createAccessControl } from "./access";
export type ArrayElement<T> = T extends readonly (infer E)[] ? E : never;
export type SubArray<T extends unknown[] | readonly unknown[] | any[]> =
| T[number][]
| ReadonlyArray<T[number]>;

View File

@@ -1,5 +1,5 @@
import type { BetterAuthClientPlugin } from "@better-auth/core";
import type { AccessControl, Role } from "../access";
import type { AccessControl, ArrayElement, Role } from "../access";
import type { defaultStatements } from "./access";
import { adminAc, userAc } from "./access";
import type { admin } from "./admin";
@@ -26,7 +26,7 @@ export const adminClient = <O extends AdminClientOptions>(
type PermissionType = {
[key in keyof Statements]?: Array<
Statements[key] extends readonly unknown[]
? Statements[key][number]
? ArrayElement<Statements[key]>
: never
>;
};

View File

@@ -15,7 +15,7 @@ import {
} from "../../cookies";
import { parseSessionOutput, parseUserOutput } from "../../db/schema";
import { getDate } from "../../utils/date";
import type { AccessControl } from "../access";
import type { AccessControl, ArrayElement } from "../access";
import type { defaultStatements } from "./access";
import { ADMIN_ERROR_CODES } from "./error-codes";
import { hasPermission } from "./has-permission";
@@ -1603,7 +1603,7 @@ export const userHasPermission = <O extends AdminOptions>(opts: O) => {
type PermissionType = {
[key in keyof Statements]?: Array<
Statements[key] extends readonly unknown[]
? Statements[key][number]
? ArrayElement<Statements[key]>
: never
>;
};

View File

@@ -11,7 +11,7 @@ import type {
} from "../../plugins/organization/schema";
import type { BetterAuthOptions, BetterAuthPlugin } from "../../types";
import type { Prettify } from "../../types/helper";
import type { AccessControl, Role } from "../access";
import type { AccessControl, ArrayElement, Role } from "../access";
import type { defaultStatements } from "./access";
import { adminAc, defaultRoles, memberAc, ownerAc } from "./access";
import { ORGANIZATION_ERROR_CODES } from "./error-codes";
@@ -95,7 +95,7 @@ export const organizationClient = <CO extends OrganizationClientOptions>(
type PermissionType = {
[key in keyof Statements]?: Array<
Statements[key] extends readonly unknown[]
? Statements[key][number]
? ArrayElement<Statements[key]>
: never
>;
};

View File

@@ -5,7 +5,7 @@ import { APIError } from "@better-auth/core/error";
import * as z from "zod";
import { getSessionFromCtx } from "../../api";
import { shimContext } from "../../utils/shim";
import type { AccessControl } from "../access";
import type { AccessControl, ArrayElement } from "../access";
import type { defaultStatements } from "./access";
import { defaultRoles } from "./access";
import { getOrgAdapter } from "./adapter";
@@ -183,7 +183,7 @@ const createHasPermission = <O extends OrganizationOptions>(options: O) => {
type PermissionType = {
[key in keyof Statements]?: Array<
Statements[key] extends readonly unknown[]
? Statements[key][number]
? ArrayElement<Statements[key]>
: never
>;
};