fix: return type of $

This commit is contained in:
EnixCoda 2019-02-20 12:30:20 +08:00
parent b877c258cc
commit e4e2389868
No known key found for this signature in database
GPG key ID: 6825847C88AA329A
3 changed files with 12 additions and 6 deletions

View file

@ -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<Props> {

View file

@ -37,7 +37,7 @@ export type ConnectorState = {
compressSingletonFolder?: boolean
copyFileButton?: boolean
copySnippetButton?: boolean
logoContainerElement: HTMLElement | null
logoContainerElement: Element | null
init: () => void
onPJAXEnd: () => void

View file

@ -28,11 +28,17 @@ function setBodyIndent(shouldShowGitako: boolean) {
}
}
function $<R1 = never, R2 = never>(
function $<E extends (element: Element) => 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<O> | null
: O extends never
? (ReturnType<E> | null)
: ReturnType<O> | ReturnType<E> {
const element = document.querySelector(selector)
if (element) {
return existCallback ? existCallback(element) : element