The type definition for Traverson is structured in the following way:
declare var traverson: Traverson.TraversonMethods;
export = traverson;
declare namespace Traverson {
interface TraversonMethods {
from(uri: string): Builder;
}
interface Builder {
getResource(callback: (err: any, document: any, traversal?: Traversal) => void): InAction;
// many more methods
}
interface InAction { /* not relevant here */}
interface Traversal { /* not relevant here */}
}
When using anonymous functions as parameters for getResource()
, everything works well. However, I would like to use a named function and define its signature accurately. This requires access to the Traversal
interface, which is not exported.
To work around this issue, I have implemented the following solution:
import traverson from 'traverson';
export type Builder = ReturnType<typeof traverson.from>;
let b: Builder; // only used in next line
export type Traversal = NonNullable<Parameters<Parameters<typeof b.getResource>[0]>[2]>;
Although this workaround seems effective, I am not satisfied with the declaration of a variable on the third line. It feels unnecessary to include this in the transpiled Javascript code just to obtain a type declaration.
Is there a more efficient method to make the Traversal
interface accessible to my code? (Apart from submitting a PR for the source of that typedef, of course.)