Closed anlauren closed 3 years ago
Hey, very strange. I’ve noticed there is no kind property on your interfaces.
Except of that the rest should be fine. Please specify ts version
update: ah I see. Will check it tomorrow
these are my different packages installed:
"ts-jest": "^26.4.3",
"ts-loader": "^8.0.8",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.0.5"
Adding the "kind" doesn't change anything, i've tried with every combination i could but i went back to the example in the docs to report the issue and be sure i was not doing something stupid
Okay i'm reading the implementation and I don't understand how it's supposed to work 🤔
export declare type Subject = AnyRecord | SubjectType;
-> Either it's a record or a SubjectType. A record can accept any key as value accessor, so something of type Article
cannot be of type AnyRecord
So it has to be of type SubjectType
.
export declare type SubjectType = string | SubjectClass;
-> well it's not a string so let's move on
export declare type SubjectClass<N extends string = string> = AnyClass & {
modelName?: N;
};
-> so first it has to be AnyClass
export declare type AnyClass<ReturnType = any> = new (...args: any[]) => ReturnType;
That is a classic definition of class typechecking, which would work for typeof Article
, but is not true for Article
since it's an instance.
Hence, i have no idea how it's supposed to work 🤔 I don't know the library enough to understand it all though.
I don't see the point of & {modelName?: N;}
we're adding an optional constraint?
the only way i found to make that work is quite hacky, it's to turn either the class or the interface into a record. But that allows for unsafe practices (adding random things to the class/object is permitted and reading them as well)
import { Ability } from '@casl/ability';
interface IArticle {
[k: string]: any;
id: string;
}
class Article implements IArticle {
[k: string]: any;
constructor(public id: string) {}
}
type Action = 'create' | 'read' | 'update' | 'delete';
type Subject = Article;
const ability = new Ability<[Action, Subject]>();
const a: IArticle = {
hello: 'nope',
id: 'fd',
};
const article = new Article('test');
article.nope = 'works i know';
console.log('That is really unsafe', article.something.else);
ability.can('create', article);
There are several examples in casl-examples repo that do the same or similar. For example for react: https://github.com/stalniy/casl-examples/blob/master/packages/react-todo/src/config/ability.ts#L8
Please check them until I have time to test it by myself
I've just create a sample repo: https://github.com/stalniy/casl-ts-inference
And cannot reproduce your issue
update: use npm run build
or npm start
in that repo
I am baffled. I cloned the repo and it does work when I run but my VSCode keeps complaining about it so i guess it's on my VS code config somewhere somehow, never seen that happening before.
There was build errors as well in my project but it might have come from my experiments to try to make it work.
I'm still a bit confused about why it works and build, reading the code i mentioned above, but I guess it's on me to get better at typescript ;p
(My Vscode error in that repo if somebody sees that in the future:)
I also use vscode. Check which ts version vscode uses
There are some exceptions when interface is assignable to record. I don’t know all of them as well but looks like ts uses one of such exceptions in case of casl
OK. I close this. Feel free to comment if you find more information about this.
This happened to me also, with the root cause being that VS Code was opening using node v12 and my CLI was using node v15.
e; the implication probably being that the wrong version of typescript was installed or similar.
yes, node version cannot affect this only ts version. You need TS ^3.8.3 as far as I remember
Describe the bug When i try to run the following example in the docs:
It is raising a typescript error explaining that Type 'Article' is not assignable to type 'Subject' or Type 'Article' is not assignable to type 'AnyRecord' or Index signature is missing in type 'Article'.
I've tried to google around without luck 🤔 I'm guessing it has something to deal with how strict are the class rules but i'm using a fairly basic ts config
To Reproduce Steps to reproduce the behavior:
Expected behavior It should build
CASL Version
"@casl/ability@^5.2.2"
Environment: node: v10.22.0 "typescript": "^4.0.5"
tsconfig: