The problem is that the guy Role[]
is different from the type expected by the construction (...roles: Role[])
.
The guy Role[]
means any array whose elements are attributable to the type Role
.
Already (...roles: Role[])
is a syntactic construct that is used to define the type of a function that accepts parameters Rest, that is, a function with aridity varied.
This means that the function does not accept an array in the first argument (as you are passing), but rather a varied number of arguments of the type Role
which together form an array of the type Role[]
.
Then you need to change the type of the function so that it is no longer a variadic function:
function Roles(...roles: Role[]) {
// ...
}
Making it a function that accepts only one argument - an array of Role
s:
function Roles(roles: Role[]) {
// ...
}
See working on Typescript playground.
Another option would be to use Function.prototype.apply
or the spreading operator at the return of getRoles()
, but semantically speaking, I do not believe they are correct. Changing the expected type of function, in this case, seems to me the most appropriate.
What is this function
Roles
? Could [Edit] the question to include your signature?– Luiz Felipe
I added, the Roles function simply adds roles in the Multimedia
– Jeffyter Saraiva
What do you want
Roles
receive, in fact? An array whose elements are the keys ofroles
?– Luiz Felipe
Exactly,
Roles
must receive an array of Roles, the items in this array are keys of an object, which in this case is thekeyof typeof roles
– Jeffyter Saraiva