hello i am new to rescript, and when i want to bind some functions from JavaScript or typescript, there are some parameters with different types in front of the last parameters which are variadic arguments ( aka Rest parameters in js and ts). I wonder know If there any way to bind that and how if yes?
Can you show an example?
For example there is a function in my project with typescript we wrote before,it accept a config and some part of full route, and then generate full routes according to the config, and it’s signature showed as below:
function defineRoutes(config:Config,record: AppRouteRecordRaw): AppRouteRecordRaw[];
function defineRoutes(config:Config, records: AppRouteRecordRaw[]): AppRouteRecordRaw[];
function defineRoutes(config:Config,...otherRecords: AppRouteRecordRaw[]): AppRouteRecordRaw[];
function defineRoutes(
config: Config,
record: AppRouteRecordRaw | AppRouteRecordRaw[],
...otherRecords: AppRouteRecordRaw[]
): AppRouteRecordRaw[];
The best way is to define multiple bindings for the same function that take the different arguments
external defineRoute: (config, route) => array<route> = "defineRoute"
external defineRoutes: (config, array<route>) => array<route> = "defineRoute"
And then the actual variadic one doesn’t really matter because it’s the same as passing an array
If you look at the Belt bindings, you can see them do the same thing foten with concat()
and concatMany()
Thank you very much this really helped me!
sorry but, If there’s only one variadic arguments, what should I do to bind it? I still don’t understand
May I ask why you still want to map to this function when you have a version that already accepts records: AppRouteRecordRaw[]
? The varargs variation of the same function looks like a less reliable version of the same thing.
Usually when mapping to variadic argument lists, we’d literally just create 5-6 different bindings that each accept a fixed set of parameters. Kinda like useEffect
in the React bindings: useEffect Hook | React
not this actually example, I just want to know, If there’s a function with params of different types and the last is variadic, sometimes i meet them and there is not always accept a array to instead, that how should I resolve that? and always create multi binds is too complicated, If I use a huge numbers of params to input, how should I bind it as it is just a simple variadic param?
EDIT: Just realized we have this as well: Syntax Lookup
If the above @variadic feature doesn’t work and if you don’t like to create multiple bindings, create a JS file that exports a function that takes a single array argument and within that function, use the variadic spread to pass it and then map to it in JS. Or use a raw function to do the same thing.
Sidenote: There is a technical limitation on how many arguments you can provide in a function call, whereas a single argument with an array of elements will not be a problem due to the way things are allocated.