meteor-roles
meteor-roles copied to clipboard
Problem with Meteor.roleAssignment and typescript
Hi,
I'm trying to use meteor-roles package, but i faced a problem : Roles.userIsInRole(myId, 'admin')) always return false in client side.
I read that I had to publish Meteor.roleAssignement, but I can't because I use typescript and roleAssignment is undefined.
Can someone help me ?
Thanks
Hi,
I finally find a hack by myself. I keep this issue open, if anyone has a better idea (or if the maintainer want to close it ;)), but this is the best solution I found today.
Meteor.publish(null, function (){ var myId = Meteor.userId() if (myId && Roles.userIsInRole(myId, 'admin')) { return [Meteor.roles.find(), (Meteor as any).roleAssignment.find()] } else { return this.ready() } });
We will have to figure out how to update the Typescript typings for this. For right now you should be able to supress the warning with // @ts-ignore
Here is the types package, but we'll need to udpate it: https://www.npmjs.com/package/@types/meteor-roles
You can just add it to your meteor
namespace: const roleAssignment: Mongo.Collection<Roles.Role>;
.
This is if the type package has not yet been updated.
@FoudreNoire26i @danyhiol please check the latest version. Types have been added recently.
I have the latest version installed and I can see the types being declared if I look at the definitions file in the installed package source, but for some reason I am still getting the same error. Are they perhaps being declared incorrectly? Or am I doing something wrong?
This is the current declaration in the types for the problematic variables:
declare namespace Meteor {
var roles: Mongo.Collection<Roles.Role>
var roleAssignment: Mongo.Collection<Roles.RoleAssignment>
}
If I do:
import { Meteor } from 'meteor/alanning:roles;
Then I get the correct types, but I lose all of the other Meteor types. Perhaps this is an issue with the way zodern:types handles these d.ts files because it seems the Meteor namespace is not being extended correctly.
Looks like this is the correct definition:
declare module 'meteor/meteor' {
namespace Meteor {
const roles: Mongo.Collection<Roles.Role>
const roleAssignment: Mongo.Collection<Roles.RoleAssignment>
}
}
With the above everything works as expected.