I have been working on adding typescript definitions to a custom jasmine matcher library.
Initially, I successfully added matchers for the generic type T
. Now, my goal is to specifically add matchers for DOM elements.
While exploring the jasmine type definition code, I came across a similar technique used for ArrayLike
(refer here for the expect
overload and here for the ArrayLikeMatchers
).
Following that example, I created a similar setup.
// Overload the expect
declare function expect<T extends HTMLElement>(actual: T): jasmine.DOMMatchers<T>;
declare namespace jasmine {
// Augment the standard matchers. This WORKS!
interface Matchers<T> {
toBeExtensible(): boolean;
toBeFrozen(): boolean;
toBeSealed(): boolean;
// ... other
}
// The matchers for DOM elements. This is NOT working!
interface DOMMatchers<T> extends Matchers<T> {
toBeChecked(): boolean;
toBeDisabled(): boolean;
}
}
Despite the effort, it's currently not functioning as expected.
When testing the following code:
const div = document.createElement("div");
expect(div).toBeChecked();
The type checker throws an error:
[js] Property 'toBeChecked' does not exist on type 'Matchers'.
The apparent solution would be to include the expect
overload before the generic expect
(after the ArrayLike
overload here) in the core jasmine library.
However, that may not be feasible :)
Any suggestions on how to implement a functional solution effectively?