0

I have several typedefs that all include these fields:

* @property {String} field1 Foo bar 1
* @property {Number} field2 Foo bar 2
* @property {Number} field3 Foo bar 3
* @property {String} field4 Foo bar 4

Is there a way to define them once and shared them across the typedefs? For example, I would love to do something along the lines of:

/**
 * @typedef GenericFields
 * @property {String} field1 Foo bar 1
 * @property {Number} field2 Foo bar 2
 * @property {Number} field3 Foo bar 3
 * @property {String} field4 Foo bar 4
 */

/**
 * @typedef SomeType
 * @property {String} nonGenericField Bar foo
 * ...{GenericFields}
 */

Is something like that possible?

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128

1 Answers1

1

I suspect you may want a record (or an interface):

https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler#interface-record

A record and an interface define the same idea. They are both used to decide if a function or class implements everything it needs to be interoperable with another.

Interfaces accomplish this through a name; the condition for a valid interface is met only when the same name is used.

Records accomplish this through a structure; the condition for a valid record is met only when the structures match.

You implement interfaces (and records) with @extends {Type}.

Graham P Heath
  • 7,009
  • 3
  • 31
  • 45
  • I know that all functions are effectively just special objects, but will this syntax work with plain objects like what I have? I can't find any examples of that use case. Will look into this more and get back, thanks! – Matthew Herbst Oct 24 '18 at 20:05
  • It should... Let me know, will ya? – Graham P Heath Oct 25 '18 at 01:37