clutz icon indicating copy to clipboard operation
clutz copied to clipboard

Replace goog.forwardDeclare with imports

Open iteriani opened this issue 9 years ago • 0 comments

Historically, developers have been able to "import a namespace" by type without forcing a runtime require by using goog.forwardDeclare.

goog.module('a'); const p = goog.forwardDeclare('ns.p');

/** @param {p} a */ exports = function r(a) {}

This will make it so that a doesn't force a runtime dependency on ns.p. The contract for type checking is that somewhere in the compilation unit, ns.p eventually is included (or bad things happen).

In TypeScript this doesn't exist. You can import for a type, and internally they are transformed into goog.forwardDeclare if a runtime dependency is unnecessary. We can just translate these into imports.

Note that this may cause circular dependencies (another reason why people use forwardDeclares), ex:

goog.module('ns.p'); const r = goog.require('a');

exports = class P {} r(new P());

This needs to be resolved using interfaces.

iteriani avatar Sep 11 '17 18:09 iteriani