add ts config: noImplicitThis

This commit is contained in:
timongh 2023-01-29 18:06:00 +08:00
parent 1c9e0d0aac
commit fadc39fc5c
6 changed files with 68 additions and 60 deletions

View File

@ -23,12 +23,12 @@ export const nextPageMixin = <MappedItem extends { id: string }, RawItem>(
data() {
return {
loading: true,
cards: [],
cards: [] as MappedItem[],
hasMorePage: true,
}
},
computed: {
sortedCards() {
sortedCards(): MappedItem[] {
return ([...this.cards] as MappedItem[]).sort(descendingStringSort(it => it.id))
},
},
@ -42,6 +42,9 @@ export const nextPageMixin = <MappedItem extends { id: string }, RawItem>(
},
methods: {
async nextPage() {
const this0 = this as typeof this & {
onCardsUpdate?: (cards: MappedItem[]) => MappedItem[]
}
try {
const cards: MappedItem[] = this.sortedCards
const lastCardID = cards[cards.length - 1]?.id ?? 0
@ -61,8 +64,8 @@ export const nextPageMixin = <MappedItem extends { id: string }, RawItem>(
.filter(card => !isPreOrderedVideo(card)),
)
if (concatCards.length > 0 && this.onCardsUpdate) {
concatCards = this.onCardsUpdate(concatCards)
if (concatCards.length > 0 && this0.onCardsUpdate) {
concatCards = this0.onCardsUpdate(concatCards)
}
console.log('nextPage get', concatCards)
this.cards = concatCards

View File

@ -3,9 +3,9 @@ import { getJson } from '@/core/ajax'
/**
* API , 使 parseJson JSON , this.items
*/
export const requestMixin = (
export const requestMixin = <D, I>(
config: {
requestMethod?: (url: string) => Promise<any>
requestMethod?: (url: string) => Promise<D>
} = {},
) => {
const { requestMethod = getJson } = config
@ -18,28 +18,28 @@ export const requestMixin = (
},
data() {
return {
items: [],
items: [] as I[],
loading: true,
error: false,
}
},
computed: {
loaded() {
loaded(): boolean {
return !this.loading && !this.error
},
},
created() {
this.reload()
this.reload().then()
},
methods: {
async reload() {
const this0 = this as typeof this & { parseJson: (json: D) => I[]; itemLimit?: number }
try {
this.error = false
this.loading = true
this.items = this.parseJson(await requestMethod(this.api)).slice(
0,
this.itemLimit ?? Infinity,
)
this.items = this0
.parseJson(await requestMethod(this.api))
.slice(0, this0.itemLimit ?? Infinity)
} catch (error) {
console.error(error)
this.error = true

View File

@ -14,7 +14,7 @@ export const createEpisodesPicker = (
Vue.extend({
computed: {
checkedInputItems() {
return this.$refs.picker.checkedInputItems
return (this.$refs.picker as any).checkedInputItems
},
},
render(createElement) {

View File

@ -4,7 +4,7 @@ import type { VueModule } from '../common-types'
* video WasmPlayer , bwp-video , video
* @param selector
*/
export const bwpVideoFilter = (selector: string) => {
export const bwpVideoFilter = (selector: string): string => {
// if (!unsafeWindow.__ENABLE_WASM_PLAYER__) {
// return selector
// }
@ -24,44 +24,46 @@ interface DocumentQuerySelector {
* `document.querySelector`, `<bwp-video>`
* @param selector
*/
(selector: string): Element | null
<E extends Element>(selector: string): E | null
/**
* `querySelector`, `<bwp-video>`
* @param selector
* @param element
* @param scopedSelector
*/
(element: Element, scopedSelector: string): Element | null
<E extends Element>(element: Element, scopedSelector: string): E | null
}
export const dq: DocumentQuerySelector = (
export const dq: DocumentQuerySelector = <E extends Element>(
selectorOrElement: string | Element,
scopedSelector?: string,
) => {
): E => {
if (!scopedSelector) {
return document.querySelector(bwpVideoFilter(selectorOrElement as string))
return document.querySelector<E>(bwpVideoFilter(selectorOrElement as string))
}
return (selectorOrElement as Element).querySelector(bwpVideoFilter(scopedSelector))
return (selectorOrElement as Element).querySelector<E>(bwpVideoFilter(scopedSelector))
}
interface DocumentQuerySelectorAll {
/**
* `document.querySelectorAll` (), `<bwp-video>`
* @param selector
*/
(selector: string): Element[]
<E extends Element>(selector: string): E[]
/**
* `querySelectorAll` (), `<bwp-video>`
* @param selector
* @param scopedSelector
*/
(element: Element, scopedSelector: string): Element[]
<E extends Element>(element: Element, scopedSelector: string): E[]
}
export const dqa: DocumentQuerySelectorAll = (
export const dqa: DocumentQuerySelectorAll = <E extends Element>(
selectorOrElement: Element | string,
scopedSelector?: string,
) => {
): E[] => {
if (!scopedSelector) {
return Array.from(document.querySelectorAll(bwpVideoFilter(selectorOrElement as string)))
return Array.from(document.querySelectorAll<E>(bwpVideoFilter(selectorOrElement as string)))
}
return Array.from((selectorOrElement as Element).querySelectorAll(bwpVideoFilter(scopedSelector)))
return Array.from(
(selectorOrElement as Element).querySelectorAll<E>(bwpVideoFilter(scopedSelector)),
)
}
interface DocumentEvaluate {
(xpathExpression: string): XPathResult
@ -74,8 +76,8 @@ export const de: DocumentEvaluate = (
contextNode?: Node,
type?: number,
result?: XPathResult,
) => document.evaluate(xpathExpression, contextNode, null, type, result)
interface DocumentEvaluateAll {
): XPathResult => document.evaluate(xpathExpression, contextNode, null, type, result)
type DocumentEvaluateAll = {
(xpathExpression: string): Node[]
(xpathExpression: string, contextNode: Node): Node[]
(xpathExpression: string, contextNode: Node, order: boolean): Node[]
@ -86,7 +88,7 @@ export const dea: DocumentEvaluateAll = (
contextNode?: Node,
order?: boolean,
result?: XPathResult,
) => {
): Node[] => {
const xpathResult = de(
xpathExpression,
contextNode,
@ -96,18 +98,12 @@ export const dea: DocumentEvaluateAll = (
return Array.from({ length: xpathResult.snapshotLength }, (_, i) => xpathResult.snapshotItem(i))
}
interface DocumentEvaluateAllIterable {
(xpathExpression: string): Iterable<Node>
(xpathExpression: string, contextNode: Node): Iterable<Node>
(xpathExpression: string, contextNode: Node, order: boolean): Iterable<Node>
(xpathExpression: string, contextNode: Node, order: boolean, result: XPathResult): Iterable<Node>
}
export const deai: DocumentEvaluateAllIterable = (
function* deaiRaw(
xpathExpression: string,
contextNode?: Node,
order?: boolean,
result?: XPathResult,
) => {
): Generator<Node, void, void> {
const xpathResult = de(
xpathExpression,
contextNode,
@ -115,20 +111,28 @@ export const deai: DocumentEvaluateAllIterable = (
result,
)
return {
[Symbol.iterator]: () => ({
next: () => {
let node = null
do {
for (;;) {
node = xpathResult.iterateNext()
return node
? ({ done: false, value: node } as { done: false; value: Node })
: ({ done: true } as { done: true; value: any })
} while (node)
},
}),
if (node) {
yield node
} else {
break
}
}
}
interface DocumentEvaluateAllIterable {
(xpathExpression: string): IterableIterator<Node>
(xpathExpression: string, contextNode: Node): IterableIterator<Node>
(xpathExpression: string, contextNode: Node, order: boolean): IterableIterator<Node>
(
xpathExpression: string,
contextNode: Node,
order: boolean,
result: XPathResult,
): IterableIterator<Node>
}
export const deai: DocumentEvaluateAllIterable = deaiRaw
interface DocumentEvaluateSingle {
<T extends Node>(xpathExpression: string): T | null
<T extends Node>(xpathExpression: string, contextNode: Node): T | null
@ -138,7 +142,7 @@ export const des: DocumentEvaluateSingle = <T extends Node>(
xpathExpression: string,
contextNode?: Node,
result?: XPathResult,
) =>
): T | null =>
de(xpathExpression, contextNode, XPathResult.FIRST_ORDERED_NODE_TYPE, result)
.singleNodeValue as T | null
/** 空函数 */
@ -300,7 +304,7 @@ export const createHook = <ParentType, HookParameters extends any[], ReturnType
hookFunc: (...args: HookParameters) => boolean,
) => {
const original: (...args: HookParameters) => ReturnType = type[target] as any
type[target] = function hook(...args: HookParameters) {
type[target] = function hook(this: any, ...args: HookParameters) {
const shouldCallOriginal = hookFunc(...args)
if (!shouldCallOriginal) {
return undefined
@ -321,7 +325,7 @@ export const createPostHook = <ParentType, HookParameters extends any[], ReturnT
hookFunc: (...args: HookParameters) => unknown,
) => {
const original: (...args: HookParameters) => ReturnType = type[target] as any
type[target] = function hook(...args: HookParameters) {
type[target] = function hook(this: any, ...args: HookParameters) {
const result = original?.call(this, ...args)
hookFunc(...args)
return result

View File

@ -64,7 +64,7 @@ export const ScopedConsoleCallHook = 'scopedConsole.call'
* ScopedConsole,
* @param config scope
*/
export const useScopedConsole = (config: ScopedConsoleConfig | string) => {
export const useScopedConsole = (config: ScopedConsoleConfig | string): Console => {
const { before: beforeCreate, after: afterCreate } = getHook(ScopedConsoleCreateHook)
const {
name,
@ -78,7 +78,7 @@ export const useScopedConsole = (config: ScopedConsoleConfig | string) => {
target: (...args: any[]) => void,
prependConfig: ScopedConsoleConfig,
firstColor = prependConfig.color,
) => {
): ((this: Console, ...args: unknown[]) => unknown) => {
const lastScopedData: ScopedData = target[ScopedConsoleSymbol]
const backgroundColor =
(lastScopedData ? prependConfig.color : firstColor) ?? specialPalette.default
@ -98,7 +98,7 @@ export const useScopedConsole = (config: ScopedConsoleConfig | string) => {
original: lastScopedData?.original ?? target,
}
const rootTarget = currentScopedData.original
const patchedLog = function patchedLog(...args: any[]) {
const patchedLog = function patchedLog(this: Console, ...args: unknown[]): unknown {
const hookPayload = {
type: rootTarget[NamePatchSymbol],
args,
@ -126,9 +126,9 @@ export const useScopedConsole = (config: ScopedConsoleConfig | string) => {
groupConfig: ScopedConsoleConfig,
firstColor = groupConfig.color,
counter: (num: number) => number = n => n,
) => {
): ((this: Console, ...args: unknown[]) => unknown) => {
const patch = prependBadge(target, groupConfig, firstColor)
const patchedGroup = function patchedGroup(...args: any[]) {
const patchedGroup = function patchedGroup(this: Console, ...args: unknown[]): unknown {
const returnValue = patch.apply(this, args)
groupCounter = counter(groupCounter)
return returnValue
@ -179,7 +179,7 @@ export const useScopedConsole = (config: ScopedConsoleConfig | string) => {
)
scopedConsole.debug = (() => {
const patch = prependBadge(console.debug, actualConfig)
return function patchedDebug(...args: any[]) {
return function patchedDebug(this: Console, ...args: unknown[]): unknown {
if (!getGeneralSettings().devMode) {
return undefined
}

View File

@ -11,6 +11,7 @@
"target": "ESNext",
"strict": false,
"noImplicitAny": false,
"noImplicitThis": true,
"importsNotUsedAsValues": "error",
"sourceMap": true,
"baseUrl": ".",