1

I am writing a DefinitelyTyped lib for a js library. The library in question implements a lot of callbacks, that I believe could be solved with generics.

The question is similar to How to document a callback function parameter in typescript but I felt like my case could be an XYZ problem case, where I am trying to figure why my solution does not work, when there simply could be a much more simpler solution.


class SomeClass {

 loadResourceTypeA(resourceTypeA: TypeA, options: OptionsType, successCallback: (d: Data) => void, errorCallback: (resource: TypeA, error: Error) => void);

 loadResourceTypeB(resourceTypeB: TypeB, options: OptionsType, successCallback: (d: Data) => void, errorCallback: (resource: TypeB, error: Error) => void);

 loadResourceTypeC(resourceTypeC: TypeC, options: OptionsType, successCallback: (d: Data) => void, errorCallback: (resource: TypeC, error: Error) => void);

}

as evident, the three function could simply be solved with generics.. minus the documentation.

as of now, my current solution is to

type LoadResource<T> = (resource: T, options: OptionsType, successCallback: (d:Data) => void, errorCallback: (resource: T, error: Error) => void) => void;

class SomeClass {
 /**
  * @param ...
 */
 loadResourceTypeA: LoadResource<TypeA>;
/**
  * @param ...
 */
 loadResourceTypeB: LoadResource<TypeB>;
/**
  * @param ...
 */
 loadResourceTypeC: LoadResource<TypeC>;
}

The issue is the type hint in vscode shows the type LoadResource<TypeA> for loadResourceTypeA with no clue of how many params it takes. (apart from the tsdoc)

Is there anyway to achieve a simple solution without having to write the same thing multiple times?

Blaine
  • 576
  • 9
  • 30
  • Please add https://www.typescriptlang.org/play with working example and not-working example how you'd imagine it'd worked if it'd work perfectly ("expected: ..." comment basically) – Dimava Jul 08 '23 at 22:16

0 Answers0