google / google/closure-compiler
Allow invokable type
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Hi,
Trying to generate externs for knockoutjs, found out that currently types (classes) can not be invoked. The next best thing is to define a `typedef` of a function and add properties:
``` javascript
/** @typedef function(?=):? */
ko.Observable;
/**
* @param {function(?)} callback
* @param {Object=} opt_scope
* @param {string=} opt_event
*/
ko.Observable.subscribe = function(callback, opt_scope, opt_event) {};
```
But two problems arise:
1. The subscribe function is not recognized in compiling code.
2. Can't have generic types (the parameter and returned value of `ko.Observable` are the same type).
This is for [knockoutjs observables](http://knockoutjs.com/documentation/observables.html) which are integrated pretty straight forward with the closure library and compiler with the exception of the generic condition of them.
I belive the main concept not grasped about `ko.observable` is mixing functions with objects.
What I'm doing right now is defining `ko.Observable` as a class with a `call` method instead of the `typedef` above. And instead of invoking the function: `observable()` I'm calling the `observable.call()` method, which I'm defining in the externs.
Is it possible to solve this with mixins? Is it better to define an invokable type? Does it exist already an option to type hint the compiler about `ko.observable`?
thank you
Contributor guide
Assessment
This issue has not been assessed yet.