fix: correct type of $

This commit is contained in:
EnixCoda 2022-06-03 14:29:06 +08:00
parent 6c671da520
commit dcaa58a31b
2 changed files with 6 additions and 6 deletions

View file

@ -7,8 +7,8 @@ import { CopyFileButton, copyFileButtonClassName } from './CopyFileButton'
export function resolveMeta(): Partial<MetaData> {
const metaData = {
userName: $('[itemprop="author"] > a[rel="author"]', e => e.textContent?.trim()),
repoName: $('[itemprop="name"] > a[href]', e => e.textContent?.trim()),
userName: $('[itemprop="author"] > a[rel="author"]', e => e.textContent?.trim()) || undefined,
repoName: $('[itemprop="name"] > a[href]', e => e.textContent?.trim()) || undefined,
branchName: getCurrentBranch(true),
}
if (!metaData.userName || !metaData.repoName) {
@ -65,7 +65,7 @@ export function getCurrentBranch(passive = false) {
const findFileButtonSelector =
'#js-repo-pjax-container .repository-content .file-navigation a[data-hotkey="t"]'
const urlFromFindFileButton: string | undefined = $(
const urlFromFindFileButton = $(
findFileButtonSelector,
element => (element as HTMLAnchorElement).href,
)

View file

@ -38,17 +38,17 @@ export function setBodyIndent(shouldShowGitako: boolean) {
}
export function $(selector: string): HTMLElement | null
export function $<T1>(selector: string, existCallback: (element: HTMLElement) => T1): T1
export function $<T1>(selector: string, existCallback: (element: HTMLElement) => T1): T1 | null
export function $<T1, T2>(
selector: string,
existCallback: (element: HTMLElement) => T1,
otherwise: () => T2,
): T1 | T2
): T1 | T2 | null
export function $<T2>(
selector: string,
existCallback: undefined | null,
otherwise: () => T2,
): HTMLElement | null | T2
): HTMLElement | T2 | null
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function $(selector: string, existCallback?: any, otherwise?: any) {
const element = document.querySelector(selector)