casl: error on new Ability(rules)

Hi, I’m using casl angular and have alredy update the version from 3.X to 4. I have updated my code a little but doesn’t work.

Current version: “@casl/ability”: “^4.0.8”, “@casl/angular”: “^4.0.4”,

private defineAbilityFor(user: Employee) {
    const { can, cannot, rules } = new AbilityBuilder();
    if (user.isAdmin) {
      can('manage', 'all');
    } else if (user.isUser) {
      can('read', 'all');
      for (const projectData of user.MandatoryProjectList) {
        can(['share', 'edit', 'assign'], 'Project', { idProject: projectData.idProject });
      }
      cannot(['edit', 'delete'], Company);
    }

    // return rules;
    return new Ability(rules);
  }

Returns a error on the line return new Ability(rules);

ERROR in src/app/services/auth.service.ts:86:24 - error TS2345: Argument of type ‘(ClaimRawRule<string> | LegacyClaimRawRule<string> | SubjectRawRule<string, SubjectType, unknown> | LegacySubjectRawRule<string, SubjectType, unknown>)[]’ is not assignable to parameter of type ‘(SubjectRawRule<string, SubjectType, unknown> | LegacySubjectRawRule<string, SubjectType, unknown>)[]’. Type ‘ClaimRawRule<string> | LegacyClaimRawRule<string> | SubjectRawRule<string, SubjectType, unknown> | LegacySubjectRawRule<string, SubjectType, unknown>’ is not assignable to type ‘SubjectRawRule<string, SubjectType, unknown> | LegacySubjectRawRule<string, SubjectType, unknown>’. Type ‘ClaimRawRule<string>’ is not assignable to type ‘SubjectRawRule<string, SubjectType, unknown> | LegacySubjectRawRule<string, SubjectType, unknown>’. Type ‘ClaimRawRule<string>’ is not assignable to type ‘SubjectRawRule<string, SubjectType, unknown>’. Property ‘subject’ is optional in type ‘ClaimRawRule<string>’ but required in type ‘SubjectRawRule<string, SubjectType, unknown>’.

86 return new Ability(rules);


Angular CLI: 9.1.4 Node: 10.15.3 OS: darwin x64

Angular: 9.1.4 … animations, cli, common, compiler, compiler-cli, core, forms … language-service, platform-browser, platform-browser-dynamic … router Ivy Workspace: Yes

Package Version

@angular-devkit/architect 0.900.7 @angular-devkit/build-angular 0.900.7 @angular-devkit/build-optimizer 0.900.7 @angular-devkit/build-webpack 0.900.7 @angular-devkit/core 9.0.7 @angular-devkit/schematics 9.1.4 @angular/cdk 9.2.2 @angular/flex-layout 9.0.0-beta.29 @angular/material 9.2.2 @angular/material-moment-adapter 9.2.2 @ngtools/webpack 9.0.7 @schematics/angular 9.1.4 @schematics/update 0.901.4 rxjs 6.5.5 typescript 3.7.5 webpack 4.41.2

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Starting from 4.0, AbilityBuilder accepts optional generic parameter TAbility, by default this parameter equals PureAbility. What you need to do is to change how you create AbilityBuilder:

const { can, cannot, rules } = new AbilityBuilder<Ability>();

For more details check the API docs