question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static Methods Are Dropped On Externalised Interfaces

See original GitHub issue

The compiler wouldn’t keep static methods even though they are defined in externs.

ExternsSource
/**
 * @fileoverview
 * @externs
 */

/**
 * @interface
 */
var Test

/**
 * @param {string} test
 */
Test.staticMethod = function(test) {}
/**
 * @param {string} test
 */
Test.prototype.api = function(test) {}

var module
/** @type {!Object} */
module.exports
import './externs'

/**
 * @implements {Test}
 */
class Example {
  api(test) {
    console.log(test)
  }
  /** @nocollapse */
  static staticMethod(test) {
    console.error(test)
  }
}

module.exports = Example

OUTPUT

'use strict';
class b {
  api(a) {
    console.log(a);
  }
  static a(a) {
    console.error(a);
  }
}
module.exports = b;
java -jar google-closure-compiler-java/compiler.jar --compilation_level \
ADVANCED --language_out ECMASCRIPT_2017 --formatting \
PRETTY_PRINT --js_output_file t/c.js 
--module_resolution NODE --js \
t/index.js t/externs.js

The title says “dropped”, and if there was no nocollapse, the static method would have been removed. The workaround is to write

WorkaroundCompiled
  /** 
    * @nocollapse
    * @suppress {checkTypes}
    */
  static 'staticMethod'(test) {
    console.error(test)
  }
'use strict';
class b {
  api(a) {
    console.log(a);
  }
  static ["staticMethod"](a) {
    console.error(a);
  }
}
module.exports = b;

OK the static method is not used, but nor is the instance method – they should both be present, unless I understand wrongly what effect the Test.staticMethod has in externs.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
brad4dcommented, Aug 14, 2019

@zavr-1 to you randomObject defined in externs and the file variable created in your example code seem completely unrelated, but the type information on them is very weak from the compiler’s point of view. Basically, they are seen as simple subclasses of Object which means its safest to assume they are related to each other.

1reaction
shickscommented, Aug 1, 2019

That’s right, good catch - implementing an interface is only about the instance.

@export may not be what you want, either. I’m not sure if there is way to write what you want. Effectively you need to define an extern’d interface for the static part of the class, and then you’d want to declare the class itself as an instance of that interface. But that could break other parts, so it’s not great.

Abstract classes do keep their statics along, but you can’t extend an extern class, since it doesn’t actually exist. Possibly you could fake it with @extends but no extends clause, though that might also cause an error. There’s talk about allowing to “implement” a class (a la TypeScript) but we don’t have anything concrete for that.

I would consider the bug here that we just don’t have a way to write this right now. But I’m also not optimistic about being able to make it possible, since it seems like it will require a pretty significant architectural change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Eclipse does not allow access to static interface method in ...
The problem was caused by using a pre-Java-8 version in Workspace B. There is no bug in Eclipse or Java, Java 7 and...
Read more >
Why were default and static methods added to interfaces in ...
static methods do not belong to interfaces. They belong to utility classes. "default" methods shouldn't have been allowed in interfaces at all. You...
Read more >
Suggestion: Add abstract static methods in classes ... - GitHub
The related problem concerns static modifier on interface methods declaration, which is disallowed. 1. The problem. 1.1. Abstract static methods ...
Read more >
Are Static Methods/Variables bad practice? - Tom Butler
In short: Yes. There are many disadvantages and static methods should almost never be used. Static methods allow procedural/functional code ...
Read more >
Java 8 Interface Changes - static method, default method
Java 8 interface changes include static methods and default methods in interfaces. Prior to Java 8, we could have only method declarations ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found