You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 22, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on May 22, 2025. It is now read-only.
Tsickle emits bound this generics which trigger jscompiler whitelist conformance checks #1015
m getting a conformance check error with a generic this value:
/**
* d3 inverts the `this` ownership. buildD3Handler reverts that, so that the
* passed method is called in the original context with the d3 value as the
* input.
*/
export function
buildD3Handler<ContextT, SVGEltT extends SVGElement, BoundObjT extends
BoundD3T, HandlerRetT>(
context: ContextT,
targetMethod: (svgElt: SVGEltT, boundObj: BoundObjT) => HandlerRetT,
): d3.ValueFn<SVGEltT, BoundObjT, HandlerRetT> {
return function(this: SVGEltT, node: BoundObjT) {
// Here 'this' is a reference to the D3 DOM Node
return targetMethod.call(context, this, node);
};
}
As far as I can tell, this is sensible:
/**
* d3 inverts the `this` ownership. buildD3Handler reverts that, so that the
* passed method is called in the original context with the d3 value as the
* input.
* @template ContextT, SVGEltT, BoundObjT, HandlerRetT
* @param {ContextT} context
* @param {function(SVGEltT, BoundObjT): HandlerRetT} targetMethod
* @return {?}
*/
function buildD3Handler(context, targetMethod) {
return (/**
* @this {SVGEltT}
* @param {BoundObjT} node
* @return {?}
*/
function (node) {
// Here 'this' is not a reference to TopologyGraphComponent object.
return targetMethod.call(context, this, node);
});
}
But I am getting:
ERROR - Violation: References to "this" that are typed as "unknown" are not allowed. See https://github.com/google/closure-compiler/wiki/Conformance:-Unknown-type-of-%E2%80%9Cthis%E2%80%9D
Reacted by Natalie Sidhom, Peter Burns and Justin Fagnani
I ran into this recently. Adding an explicit cast (e.g. this as SVGEltT}) seems to fix it.
If tsickle automatically emitted the type assertion, both on static methods (because of google/closure-compiler#3340) and when a function has an explicit this type it would do wonders for code legibility.
The reason Closure cares about what 'this' is at all is so it can consistently rename/DCE against it. For a bounded generic, we could say @this the supertype, since that has all the fields that Closure might care about. In the above that would mean emitting @this {SVGElement}.
Activity
rictic commentedon Apr 18, 2019
I ran into this recently. Adding an explicit cast (e.g.
this as SVGEltT}) seems to fix it.If tsickle automatically emitted the type assertion, both on static methods (because of google/closure-compiler#3340) and when a function has an explicit
thistype it would do wonders for code legibility.mprobst commentedon Sep 27, 2019
I see - the problem is that
SVGEltTis a template parameter, which then is?inside the method.evmar commentedon Sep 27, 2019
The reason Closure cares about what 'this' is at all is so it can consistently rename/DCE against it. For a bounded generic, we could say
@thisthe supertype, since that has all the fields that Closure might care about. In the above that would mean emitting@this {SVGElement}.evmar commentedon Sep 27, 2019
I think we do some monkeying with what 'this' refers to already in code like here:
tsickle/src/jsdoc_transformer.ts
Line 624 in e75e576
And in fact we insert explicit casts ourselves, kinda like what @rictic did, in some cases:
tsickle/src/jsdoc_transformer.ts
Line 634 in e75e576
I suspect it wouldn't be so hard to peek at the
thistype to see if it's a bounded generic and swap it out when necessary. Contributions welcome!mprobst commentedon Sep 27, 2019
evmar commentedon Sep 27, 2019
If I followed the bug, this isn't even about generic bounds really. It's that code like
fails because you can't use a templated type in a
thisclause. So the generic bounds thing doesn't even help.