0

If I define a typedef in one file, for example:

/**
 * @typedef {{
 *   prop1: string,
 *   prop2: number
 * }}
 */
myClass.typedef;

Can I share it across files? I don't want to have to declare the same typedef in every file.

I tried to do goog.provide('myClass.typedef'); in the file which defines it and goog.require('myClass.typedef'); in the other file which needs to use it. But I get an error "goog.require could not find myClass.typedef". I believe the provide/require is working in general as goog.provide('myClass'); and goog.require('myClass'); is working for the same two files.

What's the right way to do this?

Brendan
  • 56
  • 3
  • Just for sanity's sake, is the error exactly "goog.require could not find my.typedef" and not using "myClass.typedef" or is that abbreviated? – ne8il Apr 22 '13 at 16:16
  • Typo on my part. The error says "myClass.typeDef". Fixed in the original. – Brendan May 01 '13 at 18:58

2 Answers2

0

yes, typedefs are shareable, but you will need something else in the file which is included in and needed by the deps system. Before you use typedefs i would recommend you read this: http://closuretools.blogspot.co.uk/2012/02/type-checking-tips.html

lennel
  • 646
  • 3
  • 10
  • I believe I'm already doing this. As mentioned in my post, goog.provide('myClass') and goog.require('myClass') is working just fine, it's just the typeDef that generates the error. – Brendan May 01 '13 at 19:00
  • we share typedefs across the board sans any issues. Did you add a goog.provide for the typedef? It is in anycase recommended not to use typedefs. – lennel May 02 '13 at 18:55
0

you can only goog.require things that have been goog.provide-ed.

Either goog.require the class with the type set on it, or create another class such as MyClassTypes and set the type on it e.g. goog.provide('MyClass');

MyClass = function () {};

/**
 * @typedef {{
 *   prop1: string,
 *   prop2: number
 * }}
 */
MyClass.type1;

or

goog.provide('MyClassTypes');

MyClassTypes = function () {};

/**
 * @typedef {{
 *   prop1: string,
 *   prop2: number
 * }}
 */
MyClassTypes.type1;

and in the file which needs the typedef...

goog.require('MyClass');

or

goog.require('MyClassTypes');
ben schwartz
  • 2,559
  • 1
  • 21
  • 20