Extracting this suggestion from this issue:
https://github.com/Microsoft/TypeScript/issues/1336
Currently the variable arguments list supports variable arguments only as the last argument to the function:
function foo(arg1: number, ...arg2: string[]) {
}
This compiles to the following javascript:
function foo(arg1) {
var arg2 = [];
for (var _i = 1; _i < arguments.length; _i++) {
arg2[_i - 1] = arguments[_i];
}
}
However, variable argument functions are limited to appearing only as the last argument and not the first argument. I propose support be added for having a variable argument appear first, followed by one or more fixed arguments:
function subscribe(...events: string[], callback: (message: string) => void) {
}
// the following would compile
subscribe(message => alert(message)); // gets all messages
subscribe('errorMessages', message => alert(message));
subscribe(
'errorMessages',
'customMessageTypeFoo123',
(message: string) => {
alert(message);
});
// the following would not compile
subscribe(); // supplied parameters do not match any signature of call target
subscribe('a1'); // argument of type 'string' does not match parameter of type '(message: string) => void'
subscribe('a1', 'a2'); // argument of type 'string' does not match parameter of type '(message: string) => void'
subscribe compiles to the following JavaScript:
function subscribe() {
var events= [];
var callback = arguments[arguments.length - 1];
for(var _i = 0; _i < arguments.length - 2; _i++) {
events[_i] = arguments[_i];
}
}
notes: it should be impossible for typescript code to call this function with zero arguments when typechecking. If JS or untyped TS code calls it without arguments, callback will be undefined. However, the same is true of fixed arguments at the beginning of the function.
edit: used a more realistic/motivating example for the fixed-last/variable-arguments-first function.
I'm assuming by fixed arguments you mean required arguments since this certainly couldn't work when mixed with other rest params or optional arguments. While this proposal would work I'm not sure I see this adding enough value. Are there a lot of existing JavaScript APIs that would require this sort of signature? If not, this isn't really adding any expressivity or much convenience, just reordering some arguments in a subset of signatures that use rest params. Offhand I can't think of any other languages with varargs that support using them in any position but the last.
A cataloging of libraries that use this pattern would be useful.
One example is String.prototype.replace. Although this occurs in the middle and not exclusively at the beginning or end.
lib.d.ts does this:
replace(searchValue: RegExp, replaceValue: (substring: string, ...args: any[]) => string): string;
should be this:
replace (searchValue: RegExp, replaceValue: (match: string, ...refs: string[], index: number, input: string) => string): string;
I just came across this when updating TypeDefinitions for rethinkdb. getAll
(http://rethinkdb.com/api/javascript/get_all/) should look like this:
getAll(key: string, ...keys: string[]): Selection
getAll(key: string, ...keys: string[], index: {index: string}): Selection
The problem here is index
in the end being optional, but that is there problem, in an interface this still should work. Another example from rethinkdb client is the map
function (http://rethinkdb.com/api/javascript/map/) which doesn't have the last field as optional.
For now I'm kind of "solving" it like Action
and Func
are defined in C# by simply creating new functions with more parameters:
getAll(key: string, ...keys: string[]): Selection;
getAll(key: string, key2: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, key4: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, key4: string, key5: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, key4: string, key5: string, key6: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, key4: string, key5: string, key6: string, key7: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, key4: string, key5: string, key6: string, key7: string, key8: string, index?: {index: string}): Selection;
getAll(key: string, key2: string, key3: string, key4: string, key5: string, key6: string, key7: string, key8: string, key9: string, index?: {index: string}): Selection;
but this isn't just a pain to do, it's also not the representation of the underlying javascript library
As mentioned in #3686, to add to the list of libraries doing this, lodash uses this pattern in extend/assign.
I also want to call out that lodash makes the callback
and thisArg
arguments optional.
I'm getting stumped by something. Trying to create the typings for Flyd; it's a library that creates streams. It has a function combine
that combines a variable amount of streams in one. It works like this:
const combinedSignal = flyd.combine(function(s1:Signal,s2:signal2,s3:signal3....,self:signal,changed:signal[]){})
I've tried the method described by @pgrm, but I'm getting Argument of type '(s: FlydStream, self: FlydStream) => void' is not assignable to parameter of type 'FlydCombinator'
, even if one of the overloaded calls matches this pattern exactly.
It seems the method works as long as I don't have another interface with overload that uses an overloaded interface. But the library is entirely made of curried functions calling each other, I just can't not have overloads using overloads.
I think I've managed to recreate a minimal test case here
Same here.
Tried to write type definitions for an library function which accepts a list of string arguments for filtering and as a last argument a callback. Only way to solve this right now I know is to prepare many copies of the function so it can be used in most cases.
Considering this issue was first opened in Dec 2014, I think this should definitely be added in the near future!
It looks like Ember.computed
uses it.
I wonder if allowing rest arguments in _non-implementation signatures_ would be acceptable. That way we could potentially support this at the type level but we'd leave the runtime implementation to be decided by users.
lodash 4 uses it for a number of functions: _.differenceBy
, _.differenceWith
. _.xorBy
, _.xorWith
, _.intersectionBy
, _.intersectionWith
, _.unionBy
, _.unionWith
, _.zipWith
, _.assignInWith
, _.assignWith
.
koa-router also uses fixed params _after_ varargs: https://www.npmjs.com/package/koa-router#named-routes
This has comes up a lot with certain style of function calling I've seen. The following is not possible as far as I'm aware, related to this issue.
type FnWithCallbackAtEnd<
FnA extends Array<unknown>,
FnR extends unknown,
CbA extends Array<unknown>,
CbR extends unknown
> = (
...args: FnA,
cb: (...args: CbA) => CbR
) => FnR
google api client library uses these types of callbacks, params where the last argument is a callback.
In general the args are usually fixed, but I want to easily convert any callback function in this style into a promise. To do so, it'd be nice to maintain type safety throughout.
Another use case: Jest .each
function for data driven testing.
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/34617
Some personal use case here passing a context as last argument: https://stackblitz.com/edit/ts-with-rest-params
BTW, if someone have a better approach… 😊
FastCheck uses a callback at the end of the parameter list to define a property:
https://cdn.jsdelivr.net/npm/[email protected]/lib/types/check/property/Property.generated.d.ts
Reversing the argument order could help but its problematic to do that with existing APIs:
Joi also uses this pattern:
I was trying to make a function whose variable arguments are effectively a tuple of the same type, and I wanted to add an optional last parameter to allow a user to be able to optionally specify options on how to handle all the previous args (how to handle the tuple).
F.e.
someFunction(1,2,3,5,2,6,73,3,{method: 'factorial'})
I think the easiest solution, as I have the ability to modify the signature of the API, is to allow it to be called as
someFunction(1,2,3,5,2,6,73,3)
or
someFunction([1,2,3,5,2,6,73,3], options)
This is easy to do. Here's a playground example.
Another way you can do it is to track a variable in the outer scope of your function. The call site would look like this:
someFunction(1,2,3,5,2,6,73,3)
or
someFunction.callback = () => {...}
someFunction(1,2,3,5,2,6,73,3)
But remember to make someFunction
set callback
back to null
so that the next call doesn't accidentally use the last-used callback. This forces the user to always specify callback
before they call someFunction
if they need a callback.
You will almost always want to follow the error callback convention, since most Node.js users will expect your project to follow them. The general idea is that the callback is the last parameter
From https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/
Module example:
Also there is callbackify
and promisify
in the util
module.
https://nodejs.org/dist/latest-v12.x/docs/api/util.html#util_util_promisify_original
The types for promisify
are incredibly long and not complete after too many parameters: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/util.promisify/index.d.ts
I wanted this feature for typing a decorator that takes a function with a callback (node style) and add the promise api if no callback is provided. Thus, the function is now retro-compatible with the callback call and as a promise with async/await.
I wanted the types for the decorated function interface to be inferred from the original function.
While I agree that this feature is very important and long overdue, there is a good reason not to implement it: It is not compatible with the ecmascript implementation of the rest spread operator.
function x (a, ...b, c) {}
results in an error like: SyntaxError: Rest parameter must be last formal parameter
.
I couldn't find a documented reason as to why this syntax is prohibited in ecmascript. But I believe it might warrant a request to the TC39 to add this functionality.
But it still could be supported in function overloads and function types.
it almost seems like this should be possible with the new Variadic Tuple Types releasing in 4.0?
Messed around with it a bit, but couldn't get it.
Attempt
type StandardCallback<R> = (error: Error, result: R) => any
type Append<I, T extends unknown[]> = [...T, I]
function functionEndsWithCallback(...args: Append< StandardCallback<string>, unknown[]>){
}
functionEndsWithCallback(1, 2, (error) => {}); // not typesafe :(
Update
Go a response in this thread. As suspected, it can now be done!
Related question/Answer on SO
https://stackoverflow.com/questions/61273834/function-with-any-number-of-arguments-followed-by-a-callback-in-typescript/63295172#63295172
Most helpful comment
It looks like
Ember.computed
uses it.I wonder if allowing rest arguments in _non-implementation signatures_ would be acceptable. That way we could potentially support this at the type level but we'd leave the runtime implementation to be decided by users.