You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The MergeUnion<T> type in @casl/ability is overly restrictive, causing TypeScript errors when using dot-notation paths (e.g., 'tenant.id') in conditions. This limitation prevents referencing nested properties as string keys, even though CASL and MongoDB support such syntax.
Steps to Reproduce
Configure an ability:
import{AbilityBuilder,Ability}from'@casl/ability';const{ can, build }=newAbilityBuilder(Ability);can('read','User',{'tenant.id': '12345'});// Dot-notation for nested propertyconstability=build();
Check permissions:
ability.can('read','User',userData);
TypeScript raises an error:
TS2345: Argument of type `{ 'tenant.id': string }` is not assignable...
Expected Behavior
TypeScript should allow dot-notation paths without requiring type casting.
Proposed Fix
Modify MergeUnion<T> in conditions.ts to support arbitrary string keys:
This change will basically allow any stringed property to be specified in conditions. Which is not good for the case when there are properly configured types.
I’ll double check this but I had the impression that TS will infer it by default from this mapping type
Thank you for your response! I understand your concern regarding type safety and the risks of allowing arbitrary string keys.
However, I find it unfortunate that CASL does not currently support conditions with nested objects like:
can('read','User',{tenant: {id: '12345'}});
This would align better with how many ORMs and libraries handle object relationships. For example, in an ORM, we often work with nested objects to represent relationships between entities, making this approach more natural and easier to use.
Supporting nested object conditions directly in CASL would improve both usability and consistency with ORM practices. It could also help avoid relying on dot-notation strings, which are less intuitive and more error-prone.
If implementing this behavior is not feasible for now, I'd appreciate any guidance on how to structure conditions more effectively within the current limitations.
Thanks again for your efforts on this library—it’s an amazing tool for access control. 😊
Bug Description
The
MergeUnion<T>
type in@casl/ability
is overly restrictive, causing TypeScript errors when using dot-notation paths (e.g.,'tenant.id'
) in conditions. This limitation prevents referencing nested properties as string keys, even though CASL and MongoDB support such syntax.Steps to Reproduce
Expected Behavior
TypeScript should allow dot-notation paths without requiring type casting.
Proposed Fix
Modify
MergeUnion<T>
in conditions.ts to support arbitrary string keys:This change aligns TypeScript behavior with CASL’s runtime capabilities, improving developer experience while maintaining backward compatibility.
Environment
@casl/ability
: v6.7.4The text was updated successfully, but these errors were encountered: