google / google/closure-compiler
FR: template-matching on "rest" params of generic functions
- 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
Assessment
This issue has not been assessed yet.