I have a library jQuery Terminal where I’m extending the jQuery object, and I’m adding new methods.
This is part of d.ts file:
declare module 'jquery.terminal' {
export namespace JQueryTerminal {
type interpreterFunction = (this: JQueryTerminal, command: string, term: JQueryTerminal) => any;
type terminalObjectFunction = (this: JQueryTerminal, ...args: (string | number | RegExp)[]) => (void | TypeOrPromise<simpleEchoValue>);
type ObjectInterpreter = {
[key: string]: ObjectInterpreter | terminalObjectFunction;
}
type Interpreter = string | interpreterFunction | ObjectInterpreter;
...
}
export interface JQueryTerminal<TElement = HTMLElement> extends JQuery<TElement> {
set_command(command: string): JQueryTerminal;
id(): number;
...
}
const JQTerminal: (window: Window, JQuery: JQueryStatic) => void;
export default JQTerminal;
}
There are a lot of types, because this is quite complex library. The problem I have is that I can’t use jQuery methods on object typed as JQueryTerminal
.
import type { JQueryTerminal } form 'jquery.terminal';
const interpreter: JQueryTerminal.Interpreter = {
size(num: string) {
this.css('--size', num);
}
};
Got error:
Property 'css' does not exist on type 'JQueryTerminal<HTMLElement>'
But it should work, since JQueryTerminal<HTMLElement>
extends JQuery<TElement>
.
The update to types are not yet published to NPM. I’m working on new documentation, and I’ve installed the library from GitHub branch.
Let me know if you want a simplified example that you can play with. I will add one when there are no obvious mistakes in my code.