Skip to content

Commit

Permalink
Type cast to the correct types
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Feb 25, 2024
1 parent d98c884 commit a73b4c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
3 changes: 0 additions & 3 deletions dist/morphlex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 30 additions & 7 deletions src/morphlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ interface ReadOnlyNodeList<T extends Node> {
}

interface ReadOnlyNodeInterface {
readonly nodeValue: Node["nodeValue"];
readonly textContent: Node["textContent"];
readonly indeterminate: HTMLInputElement["indeterminate"];
readonly value: HTMLInputElement["value"];
readonly checked: HTMLInputElement["checked"];
readonly disabled: HTMLInputElement["disabled"];
readonly selected: HTMLOptionElement["selected"];
readonly id: Element["id"];
readonly tagName: Element["tagName"];
readonly nodeType: Node["nodeType"];
Expand All @@ -18,6 +25,8 @@ interface ReadOnlyNodeInterface {
readonly hasAttribute: Element["hasAttribute"];
readonly querySelectorAll: (query: string) => NodeListOf<Element> | ReadOnlyNodeList<Element>;
readonly parentElement: ReadOnlyNodeInterface | Element["parentElement"];
readonly hasAttributes: Element["hasAttributes"];
readonly hasChildNodes: ParentNode["hasChildNodes"];
}

type ReadOnlyNode<T extends Node> = T | ReadOnlyNodeInterface;
Expand Down Expand Up @@ -169,30 +178,44 @@ function isEqualNode(node: Node, ref: ReadOnlyNode<Node>): boolean {
// so we use type guards instead. This keeps TypeScript happy, while doing
// the necessary checks at runtime.

function isText(node: ReadOnlyNode<Node>): node is Text {
function isText(node: Node): node is Text;
function isText(node: ReadOnlyNode<Node>): node is ReadOnlyNode<Text>;
function isText(node: Node | ReadOnlyNode<Node>): boolean {
return node.nodeType === 3;
}

function isComment(node: ReadOnlyNode<Node>): node is Comment {
function isComment(node: Node): node is Comment;
function isComment(node: ReadOnlyNode<Node>): node is ReadOnlyNode<Comment>;
function isComment(node: Node | ReadOnlyNode<Node>): boolean {
return node.nodeType === 8;
}

function isElement(node: ReadOnlyNode<Node>): node is Element {
function isElement(node: Node): node is Element;
function isElement(node: ReadOnlyNode<Node>): node is ReadOnlyNode<Element>;
function isElement(node: Node | ReadOnlyNode<Node>): boolean {
return node.nodeType === 1;
}

function isInput(element: ReadOnlyNode<Element>): element is HTMLInputElement {
function isInput(element: Element): element is HTMLInputElement;
function isInput(element: ReadOnlyNode<Element>): element is ReadOnlyNode<HTMLInputElement>;
function isInput(element: Element | ReadOnlyNode<Element>): boolean {
return element.localName === "input";
}

function isOption(element: ReadOnlyNode<Element>): element is HTMLOptionElement {
function isOption(element: Element): element is HTMLOptionElement;
function isOption(element: ReadOnlyNode<Element>): element is ReadOnlyNode<HTMLOptionElement>;
function isOption(element: Element | ReadOnlyNode<Element>): boolean {
return element.localName === "option";
}

function isTextArea(element: ReadOnlyNode<Element>): element is HTMLTextAreaElement {
function isTextArea(element: Element): element is HTMLTextAreaElement;
function isTextArea(element: ReadOnlyNode<Element>): element is ReadOnlyNode<HTMLTextAreaElement>;
function isTextArea(element: Element | ReadOnlyNode<Element>): boolean {
return element.localName === "textarea";
}

function isParentNode(node: ReadOnlyNode<Node>): node is ParentNode {
function isParentNode(node: Node): node is ParentNode;
function isParentNode(node: ReadOnlyNode<Node>): node is ParentNode;

Check failure on line 218 in src/morphlex.ts

View workflow job for this annotation

GitHub Actions / JavaScript Test Action

These overloads can be combined into one signature taking `Node | ReadOnlyNode<Node>`

Check failure on line 218 in src/morphlex.ts

View workflow job for this annotation

GitHub Actions / JavaScript Test Action

These overloads can be combined into one signature taking `Node | ReadOnlyNode<Node>`
function isParentNode(node: Node | ReadOnlyNode<Node>): boolean {
return node.nodeType === 1 || node.nodeType === 9 || node.nodeType === 11;
}

0 comments on commit a73b4c7

Please sign in to comment.