stalniy / casl

CASL is an isomorphic authorization JavaScript library which restricts what resources a given user is allowed to access
https://casl.js.org/
MIT License
5.92k stars 267 forks source link

Export AddRule type from AbilityBuilder #911

Open ludwigbacklund opened 4 months ago

ludwigbacklund commented 4 months ago

When using "moduleResolution": "Bundler" in tsconfig.json for a TypeScript project, and implicitly referencing AddRule you get this error:

The inferred type of '<my-own-function>' cannot be named without a reference to '../../../node_modules/@casl/ability/dist/types/types'. This is likely not portable. A type annotation is necessary.ts(2742)

These errors can be resolved by explicitly exporting these implicitly referenced types from @casl/ability, like in this PR.

My use case is something like this:

type Can = AbilityBuilder<UserAbility>['can'];
type Cannot = AbilityBuilder<UserAbility>['cannot'];

Where I then use these two types so that I can pass around can's and cannot's from my built ability into different functions.

Thanks!

ludwigbacklund commented 4 months ago

@stalniy WDYT? :)

stalniy commented 4 months ago

Is the resulting type you get keeps all generic types and overloading of corresponding AbilityBuilder functions?

what I can tell about AbilityBuilder types is that they are super complicated and uses advanced type system features and HKT hacks in order to produce desired outcome. So, I’d not like to spread this type because very likely it may be broken with the next release (at least I can’t guarantee backward compatibility in this case)