diff --git a/.husky/pre-commit b/.husky/pre-commit index 7d0de5da..d24fdfc6 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -lint-staged +npx lint-staged diff --git a/package.json b/package.json index bde7d7ce..81f218f7 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "create-react-class": "^15.6.3", "cross-env": "^7.0.2", "dumi": "^2.1.3", - "eslint": "~7.32.0", + "eslint": "~7.31.0", "father": "^4.1.3", "glob": "^7.1.6", "husky": "^8.0.3", diff --git a/src/Dom/findDOMNode.ts b/src/Dom/findDOMNode.ts index 432ced2b..c1278001 100644 --- a/src/Dom/findDOMNode.ts +++ b/src/Dom/findDOMNode.ts @@ -1,7 +1,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; -export function isDOM(node: any) { +export function isDOM(node: any): node is HTMLElement | SVGElement { // https://developer.mozilla.org/en-US/docs/Web/API/Element // Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement return node instanceof HTMLElement || node instanceof SVGElement; @@ -14,11 +14,11 @@ export default function findDOMNode( node: React.ReactInstance | HTMLElement | SVGElement, ): T { if (isDOM(node)) { - return node as unknown as T; + return (node as unknown) as T; } if (node instanceof React.Component) { - return ReactDOM.findDOMNode(node) as unknown as T; + return (ReactDOM.findDOMNode(node) as unknown) as T; } return null; diff --git a/tests/findDOMNode.test.tsx b/tests/findDOMNode.test.tsx index be4b6533..2eb05682 100644 --- a/tests/findDOMNode.test.tsx +++ b/tests/findDOMNode.test.tsx @@ -1,6 +1,6 @@ import { render } from '@testing-library/react'; import * as React from 'react'; -import findDOMNode from '../src/Dom/findDOMNode'; +import findDOMNode, { isDOM } from '../src/Dom/findDOMNode'; describe('findDOMNode', () => { it('base work', () => { @@ -60,4 +60,18 @@ describe('findDOMNode', () => { const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); expect(findDOMNode(svg)).toBe(svg); }); + + it('isDOM type', () => { + const svg: any = document.createElementNS( + 'http://www.w3.org/2000/svg', + 'svg', + ); + + // This `getBoundingClientRect` is used for ts type check + if (isDOM(svg) && svg.getBoundingClientRect()) { + expect(true).toBeTruthy(); + } else { + expect(true).toBeFalsy(); + } + }); });