google / google/closure-compiler

FR: template-matching on "rest" params of generic functions

Open
#2,221 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

I sometimes write functions that take arbitrary functions as arguments and return functions that expect the same (or slightly modified) types of parameters. Currently there's no way to do this without loosening the type information on the returned function. Some sort of template syntax (say, `*REST`, cribbing from python) could allow more precise matching:

```js
/**
* @param {function(*REST)} func
* @return {function(number, *REST)}
* @template REST
*/
function delayed(func) {
return function(time, ...args) {
setTimeout(func.bind(this, ...args), time);
};
}
```

Another use cases is mixins (where the `@param` and `@return` would be of the form `function(new: T, *REST)`, again with the possibility of adding an extra parameter).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.