UNPKG

@casl/react

Version:

React component for CASL which makes it easy to add permissions in any React application

39 lines (38 loc) 1.09 kB
import React, { ReactNode } from 'react'; import { AbilityTuple, SubjectType, AnyAbility, Generics, Abilities, IfString } from '@casl/ability'; type AbilityCanProps<T extends Abilities, Else = IfString<T, { do: T; } | { I: T; }>> = T extends AbilityTuple ? { do: T[0]; on: T[1]; field?: string; } | { I: T[0]; a: Extract<T[1], SubjectType>; field?: string; } | { I: T[0]; an: Extract<T[1], SubjectType>; field?: string; } | { I: T[0]; this: Exclude<T[1], SubjectType>; field?: string; } : Else; interface ExtraProps { not?: boolean; passThrough?: boolean; } interface CanExtraProps<T extends AnyAbility> extends ExtraProps { children: ReactNode | ((exposes: { isAllowed: boolean; ability: T; reason: string | undefined; }) => ReactNode); } export type CanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>; declare function CanComponent<T extends AnyAbility>(props: CanProps<T>): React.ReactNode; export declare const Can: typeof CanComponent; export {};