-
-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: TypeScript Compilation Error Due to Strict MergeUnion<T> Definition in @casl/ability #1015
Comments
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 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. 😊 |
As a workaround for this issue, I'm using the following type in place of import type { MongoQuery, MongoQueryFieldOperators } from '@casl/ability';
import type { Join, NestedPaths, PropertyType } from 'mongodb';
type CaslQuery<T> = {
[Property in Join<NestedPaths<T, []>, '.'>]?:
| PropertyType<T, Property>
| MongoQueryFieldOperators<PropertyType<T, Property>>;
}; I basically took the This provides type-safety for dot-notation paths, but requires the |
@maltyxx casl just uses MongoDb query by default. In mongodb nested objects (if they don’t include operators) are compared by exact equality, including the order of fields which is not usable in real life. They don’t even recommend to use this type of query. That’s why casl don’t even support it. Since, typescript supports literal types for a long time, it’s time to integrate @matejchalk approach into library. But I don’t have ETA for this |
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: