Comments (8)
This seems like a bug in closure compiler. Looking into the type at runtime, it claims to have kind==CONSTRUCTOR when the @type
is present. If it's removed, it goes back to kind==ORDINARY.
from clutz.
Ben says
"/** @type {!Function} */ is new-able as well as callable, so in some
sense it's both a constructor and an ordinary function."
from clutz.
I didn't know that in closure all functions are newable. TS plain functions are not new-able, so we have to make it explicitly new-able with
interface ClosureFunction {
(...args: any[]):any;
new (...args: any[]): any;
}
var abstractMethod: ClosureFunction;
from clutz.
In Closure, function()
is a different type from Function
. The first is a specific function that can only be called in a specific way, with particular arguments, e.g. function()
being a function that takes no args and is void, function(string): number
being a function taking a single string, returning a number. A function that's constructable but restricts it's arguments looks like this: function(new:Foo, number, string)
. Similarly, you can also specify function(this:SomeType)
.
Function
is a weak type assertion that something is any subtype of Function
, so could or could not be callable with new
and/or any args. JSCompiler allows calling these in any way.
from clutz.
The problem seems to be that the information was lost. Introspecting the type system tells us only that the thing is modelled as a class, but not whether it was declared with @constructor.
If we make all these things a class, they will appear to be newable but not callable.
Maybe we need to emit all of them as callable-newable like @rkirov wrote above.
from clutz.
Can we get to the JSDocInfo
field? That has the @constructor
thing in it, I'd hope.
Otherwise we could also just special case on tempCtor
and fail if somebody really calls their super class tempCtor
(which violates the style guide and common sense ;-)).
from clutz.
I am changing my original proposal, since this is a rare pattern and honestly new-able + callable sounds like trouble. I suggest ignoring the new-able semantics and just outputting it as var foo: Function;
for now.
We have only seen 2 places where this is used (goog.reflect.sinkValue
and goog.abstractMethod
) and neither is very useful when called with new.
from clutz.
Yeah, this seems like a corner case, and as you can specify explicitly when something should be new
able, the intent would probably never be to actually call new
on one of these.
from clutz.
Related Issues (20)
- Filter by Custom Tags Question HOT 4
- Do you use Clutz? How? HOT 2
- error: cannot find symbol import com.google.javascript.rhino.jstype.FunctionType.Parameter; HOT 6
- Question: externsMap on goog standard library
- test class_extends_array.js FAILED after 20190115 HOT 2
- Fix externs: DOMRect
- [gents] arrow functions with a single argument are not emitted properly syntactically HOT 1
- clutz should emit 'unknown' when translating '*'
- enum aliases are incorrectly emitted in partial mode HOT 4
- inner classes not emitted when outer class is emitted with named export
- declare legacy namespace + export obj + enum create wrong output HOT 1
- Static members on function not emitted
- gents does not handle goog.requireType HOT 2
- gents should convert @this
- Event vs KeyboardEvent
- Closure's `goog.isDefAndNotNull` seem to affect type inference and make clutz emit extra 'any's HOT 3
- JsDoc on static enums is not preserved
- clutz should not emit 'abstract' modifier on interfaces
- sonatype.org is erring with 'too many requests' on CI HOT 8
- Build are failing HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clutz.