Open douglasg14b opened 3 months ago
I’m thinking whether it’s actually an issue with types.
Because if TS errors, then types are correct. And it seems as issue relates to IDE or typescript service inside IDE
VSCode correctly highlights incorrect usage. Though it gives invalid suggestion
This is how I solved it in casl:
as you can see type O
is a union of possible arguments combinations but even when I try to create an array for this union of tuples, IDE provides wrong suggestions:
In many places to not repeat the same logic, I have this CanParameters<T>
helper type which allows me to write method declaration as function test(...args: CanParameters<TAbility>): ...
and this doesn't work with function generic types
But indeed this can be improved. For example by creating an object map action => subject type + generic parameter on method:
type ActionToSubjectTypeMap<T extends [string, unknown]> = {
[K in T[0]]: T extends any
? K extends T[0]
? T[1]
: never
: never
}
type PossibleAbilities =
| ['read', 'User']
| ['create' | 'delete', 'BlogPost']
type Mapping = ActionToSubjectTypeMap<PossibleAbilities>
But I consider this as an improvement not a bug because TS fails to compile with the current behavior
Describe the bug
The union type for
Subject
appears to be incorrect when different subjcts have different actions.To Reproduce
Expected behavior
The union type for subject should be impacted by the action, and not suggest subjects that are not valid for a given action.
CASL Version
@casl/ability
: 6.7.1