From e4e23898686532d9dcfa025341075ebb77ce9d84 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Wed, 20 Feb 2019 12:30:20 +0800 Subject: [PATCH] fix: return type of $ --- src/components/Portal.tsx | 2 +- src/driver/core/SideBar.ts | 2 +- src/utils/DOMHelper.ts | 14 ++++++++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/components/Portal.tsx b/src/components/Portal.tsx index 8af7152..2747871 100644 --- a/src/components/Portal.tsx +++ b/src/components/Portal.tsx @@ -2,7 +2,7 @@ import * as React from 'react' import * as ReactDOM from 'react-dom' type Props = { - into: HTMLElement + into: Element } class Portal extends React.PureComponent { diff --git a/src/driver/core/SideBar.ts b/src/driver/core/SideBar.ts index 02a601a..ee76bce 100644 --- a/src/driver/core/SideBar.ts +++ b/src/driver/core/SideBar.ts @@ -37,7 +37,7 @@ export type ConnectorState = { compressSingletonFolder?: boolean copyFileButton?: boolean copySnippetButton?: boolean - logoContainerElement: HTMLElement | null + logoContainerElement: Element | null init: () => void onPJAXEnd: () => void diff --git a/src/utils/DOMHelper.ts b/src/utils/DOMHelper.ts index db08765..f1936ec 100644 --- a/src/utils/DOMHelper.ts +++ b/src/utils/DOMHelper.ts @@ -28,11 +28,17 @@ function setBodyIndent(shouldShowGitako: boolean) { } } -function $( +function $ any, O extends () => any>( selector: string, - existCallback?: (element: Element) => R1, - otherwise?: () => R2, -): R1 | R2 | Element { + existCallback?: E, + otherwise?: O, +): E extends never + ? O extends never + ? (Element | null) + : ReturnType | null + : O extends never + ? (ReturnType | null) + : ReturnType | ReturnType { const element = document.querySelector(selector) if (element) { return existCallback ? existCallback(element) : element