From 43719aaa60b56cbdcae0112bcb29075cf030472b Mon Sep 17 00:00:00 2001 From: feely Date: Mon, 4 Dec 2023 18:02:28 +0800 Subject: [PATCH] feat: add type check for compatible --- src/living/nodes/Node.test.ts | 8 ++++++++ src/living/nodes/Node.ts | 13 +++++++------ src/type.utils.d.ts | 12 ++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/living/nodes/Node.test.ts create mode 100644 src/type.utils.d.ts diff --git a/src/living/nodes/Node.test.ts b/src/living/nodes/Node.test.ts new file mode 100644 index 0000000..08b6897 --- /dev/null +++ b/src/living/nodes/Node.test.ts @@ -0,0 +1,8 @@ +import type { CheckStrictCompatible, Equal, Expect } from "../../type.test"; +import { NodeImpl } from './Node'; + +type IncompatibleFields = CheckStrictCompatible; + +type cases = [ + Expect>, +]; diff --git a/src/living/nodes/Node.ts b/src/living/nodes/Node.ts index c00b29a..7810c2a 100644 --- a/src/living/nodes/Node.ts +++ b/src/living/nodes/Node.ts @@ -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); } @@ -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; } @@ -509,8 +509,9 @@ export class NodeImpl extends EventTarget implements Node { removeChild(child: T): T { return this._preRemove(child as unknown as NodeImpl) as unknown as T; } - replaceChild(node: T1, child: T1): T2 { - return this._replace(node as unknown as NodeImpl, child as unknown as NodeImpl) as unknown as T2; + + replaceChild(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 diff --git a/src/type.utils.d.ts b/src/type.utils.d.ts new file mode 100644 index 0000000..c01f851 --- /dev/null +++ b/src/type.utils.d.ts @@ -0,0 +1,12 @@ +export type Equal = + (() => T extends X ? 1 : 2) extends + (() => T extends Y ? 1 : 2) ? true : false; + +export type NotEqual = true extends Equal ? false : true; +export type Expect = T; + +export type FindValidFields = { + [K in keyof T]: T[K] extends never ? never : K; +}[keyof T]; + +export type CheckStrictCompatible extends true ? never : [StandardType[k], ImplType[k]]}> = Pick>;