question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript errors upgrading CASL Prisma from 1.0.1 to 1.0.2

See original GitHub issue

Describe the bug We’re having troubling upgrading CASL Prisma from 1.0.1 to 1.0.2. Our approach follows the example in the documentation here:

https://www.npmjs.com/package/@casl/prisma

So I’m not sure what we are doing wrong or if this is a bug.

To Reproduce

Prisma file.

model DbMessage {
  tenantQid String @map(name: "tenant_qid")
  qid       String @id

  topic       String
  authorQid   String @map(name: "author_qid")

  @@map(name: "message")
}

Ability file:

type AppAbility = PrismaAbility<
  [
    string,
    Subjects<{
      Message: DbMessage;
    }>,
  ]
>;
const AppAbility = PrismaAbility as AbilityClass<AppAbility>;
const { can, cannot, build } = new AbilityBuilder(AppAbility);
can('read', 'Message', { tenantQid: 'example-qid' });

TypeScript error:

TS2769: No overload matches this call.   Overload 1 of 2, '(action: string | string[], subject: "Message" | "Message"[], conditions?: undefined): RuleBuilder<AppAbility>', gave the following error.     Argument of type '{ tenantQid: string; }' is not assignable to parameter of type 'undefined'.   Overload 2 of 2, '(action: string | string[], subject: "Message" | "Message"[], fields?: string | string[] | undefined, conditions?: undefined): RuleBuilder<AppAbility>', gave the following error.     Argument of type '{ tenantQid: string; }' is not assignable to parameter of type 'string | string[] | undefined'.       Object literal may only specify known properties, and 'tenantQid' does not exist in type 'string[]'.
image

Expected behavior Expected no TypeScript error.

Versions

@casl/ability - 5.4.4 @casl/prisma - 1.0.2 typescript - 4.6.4

Environment: Node, TypeScript

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Jul 23, 2022

Not related. You cannot name your models differently, it should be this:

type AppAbility = PrismaAbility<
  [
    string,
    Subjects<{
      DbMessage: DbMessage; // <-- here we map model name to its actual TS representation, prisma doesn't provide this info
    }>,
  ]
>;
const AppAbility = PrismaAbility as AbilityClass<AppAbility>;
const { can, cannot, build } = new AbilityBuilder(AppAbility);
can('read', 'DbMessage', { tenantQid: 'example-qid' });

So, the name MUST be DbMessage and cannot be Message because then CASL cant find related types inside Prisma types

1reaction
stalniycommented, Jun 1, 2022

Thank you for the report. I’ll take a look at it today

Read more comments on GitHub >

github_iconTop Results From Across the Web

@casl/prisma - npm
This package allows to define CASL permissions on Prisma models using Prisma WhereInput . And that brings a lot of power in terms...
Read more >
How to resolve compile errors during upgrade of Angular ...
So I tried upgrading Typescript to 3.2 and then running “ng serve”, but it gives me this error: ERROR in The Angular Compiler...
Read more >
casl - Bountysource
CASL is an isomorphic authorization JavaScript library which restricts what resources a ... Here's a repro of type errors with Prisma 3.14 (same...
Read more >
Prisma generate (typescript error) - Questions
My prisma.yml endpoint: http://localhost:4466 datamodel: datamodel.prisma hooks: post-deploy: - prisma generate generate: generator: ...
Read more >
CASL is an isomorphic authorization JavaScript library which ...
stalniy/casl, CASL (pronounced /ˈkæsəl/, like castle) is an ... as I want to compile my typescript project using tsc, it just errors out....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found