Skip to content

Commit

Permalink
feat: add type check for compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
FeelyChau committed Dec 4, 2023
1 parent 665c0f4 commit 43719aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/living/nodes/Node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { CheckStrictCompatible, Equal, Expect } from "../../type.test";
import { NodeImpl } from './Node';

type IncompatibleFields = CheckStrictCompatible<Node, NodeImpl>;

type cases = [
Expect<Equal<{}, IncompatibleFields>>,
];
13 changes: 7 additions & 6 deletions src/living/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class NodeImpl extends EventTarget implements Node {
return documentBaseURLSerialized(this._ownerDocument);
}

get parentNode(): ParentNodeImpl {
get parentNode(): ParentNode {
return domSymbolTree.parent(this);
}

Expand Down Expand Up @@ -275,15 +275,15 @@ export class NodeImpl extends EventTarget implements Node {
return this.nodeType === this.DOCUMENT_NODE ? null : this._ownerDocument as Document;
}

get nextSibling() {
get nextSibling(): ChildNode | null {
return domSymbolTree.nextSibling(this);
}

get previousSibling() {
get previousSibling(): ChildNode | null {
return domSymbolTree.previousSibling(this);
}

get parentElement() {
get parentElement(): HTMLElement | null {
const parentNode = domSymbolTree.parent(this);
return parentNode !== null && parentNode.nodeType === NodeImpl.ELEMENT_NODE ? parentNode : null;
}
Expand Down Expand Up @@ -509,8 +509,9 @@ export class NodeImpl extends EventTarget implements Node {
removeChild<T extends Node>(child: T): T {
return this._preRemove(child as unknown as NodeImpl) as unknown as T;
}
replaceChild<T1 extends Node, T2 extends Node>(node: T1, child: T1): T2 {
return this._replace(node as unknown as NodeImpl, child as unknown as NodeImpl) as unknown as T2;

replaceChild<T extends Node>(node: Node, child: T): T {
return this._replace(node as unknown as NodeImpl, child as unknown as NodeImpl) as unknown as T;
}

// https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
Expand Down
12 changes: 12 additions & 0 deletions src/type.utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false;

export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
export type Expect<T extends true> = T;

export type FindValidFields<T> = {
[K in keyof T]: T[K] extends never ? never : K;
}[keyof T];

export type CheckStrictCompatible<StandardType, ImplType extends StandardType, Checked = {[k in keyof StandardType]: Equal<StandardType[k], ImplType[k]> extends true ? never : [StandardType[k], ImplType[k]]}> = Pick<Checked, FindValidFields<Checked>>;

0 comments on commit 43719aa

Please sign in to comment.