Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare function as a parameter in Typings Declaration

In my globals.d.ts I export multiple functions whose parameters are also functions. An example can be viewed below:

/**
 * Does something
 * @param {function(string): void} bar - a parameter
 * @returns {void}
 */
export function foo(bar: function(string): void): void;

ESLint is showing multiple errors stating that:JSDoc types can only be used inside documentation comments.. I tried fixing this error by removing the types from the function and only stating them in the comments, but then the type of the parameter becomes any (*). I also tried adding the "valid-jsdoc": "off" rule in the .eslintrc.jsonfile, with no success. Is there a way to remove the warning without losing the type of the parameter?

like image 990
nick zoum Avatar asked Aug 05 '26 13:08

nick zoum


1 Answers

You can't specify parameter type as function(string): void. It's just wrong syntax to define function types in TypeScript.
Instead of:
export function foo(bar: function(string): void): void;
do:
export function foo(bar: (arg: string) => void): void;

like image 70
cezn Avatar answered Aug 07 '26 04:08

cezn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!