Skip to content

Commit

Permalink
fix: stricter types
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 20, 2024
1 parent 4a61d33 commit a086e8b
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 122 deletions.
2 changes: 1 addition & 1 deletion packages/.test/mount.ts
Expand Up @@ -2,7 +2,7 @@ import type { InjectionKey, Ref } from 'vue-demi'
import { createApp, defineComponent, h, provide, ref } from 'vue-demi'

type InstanceType<V> = V extends { new (...arg: any[]): infer X } ? X : never
type VM<V> = InstanceType<V> & { unmount(): void }
type VM<V> = InstanceType<V> & { unmount: () => void }

export function mount<V>(Comp: V) {
const el = document.createElement('div')
Expand Down
2 changes: 1 addition & 1 deletion packages/core/createReusableTemplate/index.ts
Expand Up @@ -6,7 +6,7 @@ export type DefineTemplateComponent<
Bindings extends object,
Slots extends Record<string, Slot | undefined>,
> = DefineComponent<{}> & {
new(): { $slots: { default(_: Bindings & { $slots: Slots }): any } }
new(): { $slots: { default: (_: Bindings & { $slots: Slots }) => any } }
}

export type ReuseTemplateComponent<
Expand Down
2 changes: 1 addition & 1 deletion packages/core/createTemplatePromise/index.ts
Expand Up @@ -50,7 +50,7 @@ export interface TemplatePromiseOptions {
export type TemplatePromise<Return, Args extends any[] = []> = DefineComponent<{}> & {
new(): {
$slots: {
default(_: TemplatePromiseProps<Return, Args>): any
default: (_: TemplatePromiseProps<Return, Args>) => any
}
}
} & {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/ssr-handlers.ts
Expand Up @@ -3,15 +3,15 @@ import type { Awaitable } from '@vueuse/shared'
import type { MaybeElementRef } from './unrefElement'

export interface StorageLikeAsync {
getItem(key: string): Awaitable<string | null>
setItem(key: string, value: string): Awaitable<void>
removeItem(key: string): Awaitable<void>
getItem: (key: string) => Awaitable<string | null>
setItem: (key: string, value: string) => Awaitable<void>
removeItem: (key: string) => Awaitable<void>
}

export interface StorageLike {
getItem(key: string): string | null
setItem(key: string, value: string): void
removeItem(key: string): void
getItem: (key: string) => string | null
setItem: (key: string, value: string) => void
removeItem: (key: string) => void
}

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/core/useBreakpoints/index.ts
Expand Up @@ -89,13 +89,13 @@ export function useBreakpoints<K extends string>(breakpoints: Breakpoints<K>, op
export type UseBreakpointsReturn<K extends string = string> = {
greater: (k: K) => ComputedRef<boolean>
greaterOrEqual: (k: K) => ComputedRef<boolean>
smaller(k: K): ComputedRef<boolean>
smaller: (k: K) => ComputedRef<boolean>
smallerOrEqual: (k: K) => ComputedRef<boolean>
between(a: K, b: K): ComputedRef<boolean>
isGreater(k: K): boolean
isGreaterOrEqual(k: K): boolean
isSmaller(k: K): boolean
isSmallerOrEqual(k: K): boolean
isInBetween(a: K, b: K): boolean
current(): ComputedRef<string[]>
between: (a: K, b: K) => ComputedRef<boolean>
isGreater: (k: K) => boolean
isGreaterOrEqual: (k: K) => boolean
isSmaller: (k: K) => boolean
isSmallerOrEqual: (k: K) => boolean
isInBetween: (a: K, b: K) => boolean
current: () => ComputedRef<string[]>
} & Record<K, ComputedRef<boolean>>
4 changes: 2 additions & 2 deletions packages/core/useEventListener/index.ts
Expand Up @@ -6,8 +6,8 @@ import { unrefElement } from '../unrefElement'
import { defaultWindow } from '../_configurable'

interface InferEventTarget<Events> {
addEventListener(event: Events, fn?: any, options?: any): any
removeEventListener(event: Events, fn?: any, options?: any): any
addEventListener: (event: Events, fn?: any, options?: any) => any
removeEventListener: (event: Events, fn?: any, options?: any) => any
}

export type WindowEventName = keyof WindowEventMap
Expand Down
24 changes: 12 additions & 12 deletions packages/core/useFetch/index.ts
Expand Up @@ -72,20 +72,20 @@ export interface UseFetchReturn<T> {
onFetchFinally: EventHookOn

// methods
get(): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
post(payload?: MaybeRefOrGetter<unknown>, type?: string): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
put(payload?: MaybeRefOrGetter<unknown>, type?: string): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
delete(payload?: MaybeRefOrGetter<unknown>, type?: string): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
patch(payload?: MaybeRefOrGetter<unknown>, type?: string): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
head(payload?: MaybeRefOrGetter<unknown>, type?: string): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
options(payload?: MaybeRefOrGetter<unknown>, type?: string): UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
get: () => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
post: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
put: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
delete: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
patch: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
head: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>
options: (payload?: MaybeRefOrGetter<unknown>, type?: string) => UseFetchReturn<T> & PromiseLike<UseFetchReturn<T>>

// type
json<JSON = any>(): UseFetchReturn<JSON> & PromiseLike<UseFetchReturn<JSON>>
text(): UseFetchReturn<string> & PromiseLike<UseFetchReturn<string>>
blob(): UseFetchReturn<Blob> & PromiseLike<UseFetchReturn<Blob>>
arrayBuffer(): UseFetchReturn<ArrayBuffer> & PromiseLike<UseFetchReturn<ArrayBuffer>>
formData(): UseFetchReturn<FormData> & PromiseLike<UseFetchReturn<FormData>>
json: <JSON = any>() => UseFetchReturn<JSON> & PromiseLike<UseFetchReturn<JSON>>
text: () => UseFetchReturn<string> & PromiseLike<UseFetchReturn<string>>
blob: () => UseFetchReturn<Blob> & PromiseLike<UseFetchReturn<Blob>>
arrayBuffer: () => UseFetchReturn<ArrayBuffer> & PromiseLike<UseFetchReturn<ArrayBuffer>>
formData: () => UseFetchReturn<FormData> & PromiseLike<UseFetchReturn<FormData>>
}

type DataType = 'text' | 'json' | 'blob' | 'arrayBuffer' | 'formData'
Expand Down
10 changes: 5 additions & 5 deletions packages/core/useMemoize/index.md
Expand Up @@ -70,22 +70,22 @@ export interface MemoizeCache<Key, Value> {
/**
* Get value for key
*/
get (key: Key): Value | undefined
get: (key: Key) => Value | undefined
/**
* Set value for key
*/
set (key: Key, value: Value): void
set: (key: Key, value: Value) => void
/**
* Return flag if key exists
*/
has (key: Key): boolean
has: (key: Key) => boolean
/**
* Delete value for key
*/
delete (key: Key): void
delete: (key: Key) => void
/**
* Clear cache
*/
clear (): void
clear: () => void
}
```
18 changes: 9 additions & 9 deletions packages/core/useMemoize/index.ts
Expand Up @@ -10,23 +10,23 @@ export interface UseMemoizeCache<Key, Value> {
/**
* Get value for key
*/
get(key: Key): Value | undefined
get: (key: Key) => Value | undefined
/**
* Set value for key
*/
set(key: Key, value: Value): void
set: (key: Key, value: Value) => void
/**
* Return flag if key exists
*/
has(key: Key): boolean
has: (key: Key) => boolean
/**
* Delete value for key
*/
delete(key: Key): void
delete: (key: Key) => void
/**
* Clear cache
*/
clear(): void
clear: () => void
}

/**
Expand Down Expand Up @@ -58,19 +58,19 @@ export interface UseMemoizeReturn<Result, Args extends unknown[]> {
/**
* Call memoized function and update cache
*/
load(...args: Args): Result
load: (...args: Args) => Result
/**
* Delete cache of given arguments
*/
delete(...args: Args): void
delete: (...args: Args) => void
/**
* Clear cache
*/
clear(): void
clear: () => void
/**
* Generate cache key for given arguments
*/
generateKey(...args: Args): CacheKey
generateKey: (...args: Args) => CacheKey
/**
* Cache container
*/
Expand Down
25 changes: 12 additions & 13 deletions packages/core/usePointerLock/index.ts
Expand Up @@ -7,20 +7,16 @@ import type { MaybeElementRef } from '../unrefElement'
import type { ConfigurableDocument } from '../_configurable'
import { defaultDocument } from '../_configurable'

declare global {
interface PointerLockOptions {
unadjustedMovement?: boolean
}

interface Element {
requestPointerLock(options?: PointerLockOptions): Promise<void> | void
}
}
// declare global {
// interface PointerLockOptions {
// unadjustedMovement?: boolean
// }
// }

type MaybeHTMLElement = HTMLElement | undefined | null

export interface UsePointerLockOptions extends ConfigurableDocument {
pointerLockOptions?: PointerLockOptions
// pointerLockOptions?: PointerLockOptions
}

/**
Expand All @@ -31,7 +27,7 @@ export interface UsePointerLockOptions extends ConfigurableDocument {
* @param options
*/
export function usePointerLock(target?: MaybeElementRef<MaybeHTMLElement>, options: UsePointerLockOptions = {}) {
const { document = defaultDocument, pointerLockOptions } = options
const { document = defaultDocument } = options

const isSupported = useSupported(() => document && 'pointerLockElement' in document)

Expand Down Expand Up @@ -60,15 +56,18 @@ export function usePointerLock(target?: MaybeElementRef<MaybeHTMLElement>, optio
})
}

async function lock(e: MaybeElementRef<MaybeHTMLElement> | Event, options?: PointerLockOptions) {
async function lock(
e: MaybeElementRef<MaybeHTMLElement> | Event,
// options?: PointerLockOptions,
) {
if (!isSupported.value)
throw new Error('Pointer Lock API is not supported by your browser.')

triggerElement.value = e instanceof Event ? <HTMLElement>e.currentTarget : null
targetElement = e instanceof Event ? unrefElement(target) ?? triggerElement.value : unrefElement(e)
if (!targetElement)
throw new Error('Target element undefined.')
targetElement.requestPointerLock(options ?? pointerLockOptions)
targetElement.requestPointerLock()

return await until(element).toBe(targetElement)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/useRefHistory/index.ts
Expand Up @@ -56,26 +56,26 @@ export interface UseRefHistoryReturn<Raw, Serialized> extends UseManualRefHistor
/**
* Pause change tracking
*/
pause(): void
pause: () => void

/**
* Resume change tracking
*
* @param [commit] if true, a history record will be create after resuming
*/
resume(commit?: boolean): void
resume: (commit?: boolean) => void

/**
* A sugar for auto pause and auto resuming within a function scope
*
* @param fn
*/
batch(fn: (cancel: Fn) => void): void
batch: (fn: (cancel: Fn) => void) => void

/**
* Clear the data and stop the watch
*/
dispose(): void
dispose: () => void
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/core/useScreenOrientation/index.ts
Expand Up @@ -11,11 +11,11 @@ export type OrientationType = 'portrait-primary' | 'portrait-secondary' | 'lands
export type OrientationLockType = 'any' | 'natural' | 'landscape' | 'portrait' | 'portrait-primary' | 'portrait-secondary' | 'landscape-primary' | 'landscape-secondary'

export interface ScreenOrientation extends EventTarget {
lock(orientation: OrientationLockType): Promise<void>
unlock(): void
lock: (orientation: OrientationLockType) => Promise<void>
unlock: () => void
readonly type: OrientationType
readonly angle: number
addEventListener(type: 'change', listener: (this: this, ev: Event) => any, useCapture?: boolean): void
addEventListener: (type: 'change', listener: (this: this, ev: Event) => any, useCapture?: boolean) => void
}

/**
Expand Down
40 changes: 19 additions & 21 deletions packages/core/useSpeechRecognition/types.ts
Expand Up @@ -17,9 +17,9 @@ interface SpeechGrammar {

interface SpeechGrammarList {
readonly length: number
addFromString(string: string, weight?: number): void
addFromURI(src: string, weight?: number): void
item(index: number): SpeechGrammar
addFromString: (string: string, weight?: number) => void
addFromURI: (src: string, weight?: number) => void
item: (index: number) => SpeechGrammar
[index: number]: SpeechGrammar
}

Expand All @@ -34,17 +34,17 @@ interface SpeechRecognitionEvent extends Event {
}

interface SpeechRecognitionEventMap {
'audioend': Event
'audiostart': Event
'end': Event
'error': SpeechRecognitionErrorEvent
'nomatch': SpeechRecognitionEvent
'result': SpeechRecognitionEvent
'soundend': Event
'soundstart': Event
'speechend': Event
'speechstart': Event
'start': Event
audioend: Event
audiostart: Event
end: Event
error: SpeechRecognitionErrorEvent
nomatch: SpeechRecognitionEvent
result: SpeechRecognitionEvent
soundend: Event
soundstart: Event
speechend: Event
speechstart: Event
start: Event
}

export interface SpeechRecognition extends EventTarget {
Expand All @@ -64,11 +64,9 @@ export interface SpeechRecognition extends EventTarget {
onspeechend: ((this: SpeechRecognition, ev: Event) => any) | null
onspeechstart: ((this: SpeechRecognition, ev: Event) => any) | null
onstart: ((this: SpeechRecognition, ev: Event) => any) | null
abort(): void
start(): void
stop(): void
addEventListener<K extends keyof SpeechRecognitionEventMap>(type: K, listener: (this: SpeechRecognition, ev: SpeechRecognitionEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void
removeEventListener<K extends keyof SpeechRecognitionEventMap>(type: K, listener: (this: SpeechRecognition, ev: SpeechRecognitionEventMap[K]) => any, options?: boolean | EventListenerOptions): void
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void
abort: () => void
start: () => void
stop: () => void
addEventListener: (<K extends keyof SpeechRecognitionEventMap>(type: K, listener: (this: SpeechRecognition, ev: SpeechRecognitionEventMap[K]) => any, options?: boolean | AddEventListenerOptions) => void) & ((type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void)
removeEventListener: (<K extends keyof SpeechRecognitionEventMap>(type: K, listener: (this: SpeechRecognition, ev: SpeechRecognitionEventMap[K]) => any, options?: boolean | EventListenerOptions) => void) & ((type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) => void)
}
10 changes: 5 additions & 5 deletions packages/core/useStorage/index.ts
Expand Up @@ -9,13 +9,13 @@ import { defaultWindow } from '../_configurable'
import { guessSerializerType } from './guess'

export interface Serializer<T> {
read(raw: string): T
write(value: T): string
read: (raw: string) => T
write: (value: T) => string
}

export interface SerializerAsync<T> {
read(raw: string): Awaitable<T>
write(value: T): Awaitable<string>
read: (raw: string) => Awaitable<T>
write: (value: T) => Awaitable<string>
}

export const StorageSerializers: Record<'boolean' | 'object' | 'number' | 'any' | 'string' | 'map' | 'set' | 'date', Serializer<any>> = {
Expand Down Expand Up @@ -199,7 +199,7 @@ export function useStorage<T extends(string | number | boolean | object | null)>
storage!.removeItem(key)
}
else {
const serialized = serializer.write(v)
const serialized = serializer.write(v as any)
const oldValue = storage!.getItem(key)
if (oldValue !== serialized) {
storage!.setItem(key, serialized)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/useTemplateRefsList/index.ts
Expand Up @@ -2,7 +2,7 @@ import type { Ref } from 'vue-demi'
import { onBeforeUpdate, ref } from 'vue-demi'

export type TemplateRefsList<T> = T[] & {
set(el: Object | null): void
set: (el: Object | null) => void
}

export function useTemplateRefsList<T = Element>(): Readonly<Ref<Readonly<TemplateRefsList<T>>>> {
Expand Down

0 comments on commit a086e8b

Please sign in to comment.