Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @typedef to define a specific function type

I don't know how much of this (if any) is related to Google Closure or if it's all clean jsdoc, but I tried using @typedef to define a specific type of function like this;

/**
 * @typedef {function(paramType0, paramType1):returnType} name.space.and.TypeName
 */

also tried

/**
 * @typedef {function(paramType0, paramType1):returnType} 
 */
name.space.and.TypeName;

where param and return types are the reason I want to define this specification of function. My question is this;

Why do validation tell me that the "Method is not of Function type". I know it's an option to use the type 'Function', but as far as I know, that won't allow me to define parameter and return types? Am I wrong on this as well?

I would much appreciate some assistance on this in any case. Thanks.

like image 290
NinjaTool Avatar asked Apr 06 '15 15:04

NinjaTool


1 Answers

Typical use of a typedef looks like this:

/** @typedef {function():string} */
var SomeFunctionType;

/** @param {SomeFunctionType} x This is a function */
function useFunction(x) {
  ...
}

The question is lacking some detail and I expect you actually want a @type declaration to type a value, rather than creating a type name:

/** @type {function():string} */
var someFunction = getFunctionFromSomewhere();

someFunction();

An overview of the jsdoc annotation can be found here: https://developers.google.com/closure/compiler/docs/js-for-compiler

like image 62
John Avatar answered Sep 21 '22 10:09

John