Skip to content

Commit c327966

Browse files
committed
fix: types
1 parent e914a7d commit c327966

File tree

9 files changed

+33
-308
lines changed

9 files changed

+33
-308
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ pnpm i vue-functional-ref
2828
// ...
2929
"pnpm": {
3030
"overrides": {
31-
"@vue/runtime-core>@vue/reactivity": "npm:vue-functional-ref"
32-
}
33-
}
31+
"@vue/runtime-core>@vue/reactivity": "npm:vue-functional-ref",
32+
},
33+
},
3434
}
3535
```
3636

@@ -63,9 +63,9 @@ export default {
6363
// ...
6464
"baseUrl": ".",
6565
"paths": {
66-
"@vue/reactivity": ["./node_modules/vue-functional-ref/types"]
67-
}
68-
}
66+
"@vue/reactivity": ["./node_modules/vue-functional-ref/types"],
67+
},
68+
},
6969
}
7070
```
7171

playground/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />

playground/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createApp } from 'vue'
2-
import './style.css'
32
import App from './App.vue'
3+
import './style.css'
44

55
createApp(App).mount('#app')

playground/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { defineConfig } from 'vite'
21
import vue from '@vitejs/plugin-vue'
2+
import { defineConfig } from 'vite'
33
import VueFunctionalRef from 'vue-functional-ref/vite'
44

55
// https://vitejs.dev/config/

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ export const customRef: typeof vueCustomRef = ((value: any): any =>
8282
export const readonly: typeof vueReadonly = (target: any): any =>
8383
toFunctional(vueReadonly(toRawRef(target)), true)
8484

85-
export const toRef: typeof vueToRef = (obj: any, key: string): any =>
86-
toFunctional(vueToRef(obj, key), false)
85+
export const toRef: typeof vueToRef = ((
86+
...args: Parameters<typeof vueToRef>
87+
): any => toFunctional(vueToRef(...args), false)) as any
8788

8889
export const toRefs: typeof vueToRefs = (obj: any): any => {
8990
const refs = vueToRefs(obj)

src/vite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import rollupPlugin from './rollup'
21
import esbuildPlugin from './esbuild'
2+
import rollupPlugin from './rollup'
33
import type { Plugin } from 'vite'
44

55
export default (): Plugin => ({

tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { expect, test, vi } from 'vitest'
21
import { expectTypeOf } from 'expect-type'
2+
import { expect, test, vi } from 'vitest'
33
import {
44
computed,
55
customRef,

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"types": ["node"],
1212
"strict": true,
1313
"noUnusedLocals": true,
14-
"esModuleInterop": true
14+
"esModuleInterop": true,
15+
"skipLibCheck": true
1516
},
1617
"include": ["src", "tests"]
1718
}

types.d.ts

Lines changed: 17 additions & 294 deletions
Original file line numberDiff line numberDiff line change
@@ -1,300 +1,23 @@
1-
import type {
2-
ComputedGetter,
3-
ComputedRefSymbol,
4-
DebuggerOptions,
5-
RawSymbol,
6-
ReactiveEffect,
7-
RefSymbol,
8-
RefUnwrapBailTypes,
9-
ShallowReactiveMarker,
10-
ShallowRefMarker,
11-
WritableComputedOptions,
12-
} from '@vue/reactivity/dist/reactivity'
13-
import type { IfAny } from '@vue/shared'
1+
/* eslint-disable @typescript-eslint/method-signature-style */
142

15-
export {
16-
ReactiveFlags,
17-
DebuggerEventExtraInfo,
18-
DebuggerEvent,
19-
DebuggerOptions,
20-
RefUnwrapBailTypes,
21-
ComputedGetter,
22-
ComputedSetter,
23-
WritableComputedOptions,
24-
TriggerOpTypes,
25-
shallowReadonly,
26-
resetTracking,
27-
TrackOpTypes,
28-
EffectScheduler,
29-
ReactiveEffect,
30-
EffectScope,
31-
effectScope,
32-
enableTracking,
33-
getCurrentScope,
34-
isProxy,
35-
isReactive,
36-
isReadonly,
37-
isShallow,
38-
toRaw,
39-
track,
40-
trigger,
41-
onScopeDispose,
42-
pauseTracking,
43-
ReactiveEffectRunner,
44-
stop,
45-
ReactiveEffectOptions,
46-
effect,
47-
ITERATE_KEY,
48-
ShallowRefMarker,
49-
RawSymbol,
50-
RefSymbol,
51-
ShallowReactiveMarker,
52-
ComputedRefImpl,
53-
ComputedRefSymbol,
54-
pauseScheduling,
55-
resetScheduling,
56-
} from '@vue/reactivity/dist/reactivity'
3+
export type * from '@vue/reactivity/dist/reactivity'
574

58-
declare type Primitive =
59-
| string
60-
| number
61-
| boolean
62-
| bigint
63-
| symbol
64-
| undefined
65-
| null
66-
declare type BaseTypes = string | number | boolean
67-
declare type Builtin = Primitive | Function | Date | Error | RegExp
68-
declare type WeakCollections = WeakMap<any, any> | WeakSet<any>
69-
declare type IterableCollections = Map<any, any> | Set<any>
70-
declare type CollectionTypes = IterableCollections | WeakCollections
5+
declare module '@vue/reactivity/dist/reactivity' {
6+
export interface Ref<T = any, S = T> {
7+
(): T
8+
set(value: T): void
9+
mutate(mutator: (value: T) => void): void
10+
}
7111

72-
export declare type FunctionalRef<
73-
T = any,
74-
Writable extends boolean = boolean,
75-
> = {
76-
(): T
77-
} & (Writable extends true
78-
? {
79-
set: (value: T) => void
80-
mutate: (mutator: (value: T) => void) => void
81-
}
82-
: {})
12+
export interface ComputedRef<T = any> extends BaseComputedRef<T> {
13+
set: never
14+
}
8315

84-
export declare interface ComputedRef<T = any> extends WritableComputedRef<T> {
85-
set: never
86-
readonly value: T
87-
[ComputedRefSymbol]: true
88-
}
89-
export declare function computed<T>(
90-
getter: ComputedGetter<T>,
91-
debugOptions?: DebuggerOptions,
92-
): ComputedRef<T>
93-
export declare function computed<T>(
94-
options: WritableComputedOptions<T>,
95-
debugOptions?: DebuggerOptions,
96-
): WritableComputedRef<T>
97-
98-
export declare function deferredComputed<T>(getter: () => T): ComputedRef<T>
99-
100-
export declare type CustomRefFactory<T> = (
101-
track: () => void,
102-
trigger: () => void,
103-
) => {
104-
get: () => T
105-
set: (value: T) => void
106-
}
107-
export declare function customRef<T>(factory: CustomRefFactory<T>): Ref<T>
108-
109-
export type ReadonlyRef<T> = Omit<Readonly<T>, 'set'> & FunctionalRef<T, false>
110-
111-
export declare type DeepReadonly<T> =
112-
T extends Ref<infer U>
113-
? ReadonlyRef<Ref<DeepReadonly<U>>>
114-
: T extends Builtin
115-
? T
116-
: T extends Map<infer K, infer V>
117-
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
118-
: T extends ReadonlyMap<infer K, infer V>
119-
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
120-
: T extends WeakMap<infer K, infer V>
121-
? WeakMap<DeepReadonly<K>, DeepReadonly<V>>
122-
: T extends Set<infer U>
123-
? ReadonlySet<DeepReadonly<U>>
124-
: T extends ReadonlySet<infer U>
125-
? ReadonlySet<DeepReadonly<U>>
126-
: T extends WeakSet<infer U>
127-
? WeakSet<DeepReadonly<U>>
128-
: T extends Promise<infer U>
129-
? Promise<DeepReadonly<U>>
130-
: T extends Ref<infer U>
131-
? Readonly<Ref<DeepReadonly<U>>>
132-
: T extends {}
133-
? {
134-
readonly [K in keyof T]: DeepReadonly<T[K]>
135-
}
136-
: Readonly<T>
137-
138-
export declare function isRef<T>(r: Ref<T> | unknown): r is Ref<T>
139-
140-
export declare function markRaw<T extends object>(
141-
value: T,
142-
): T & {
143-
[RawSymbol]?: true
144-
}
145-
146-
export declare function proxyRefs<T extends object>(
147-
objectWithRefs: T,
148-
): ShallowUnwrapRef<T>
149-
150-
/**
151-
* Creates a reactive copy of the original object.
152-
*
153-
* The reactive conversion is "deep"—it affects all nested properties. In the
154-
* ES2015 Proxy based implementation, the returned proxy is **not** equal to the
155-
* original object. It is recommended to work exclusively with the reactive
156-
* proxy and avoid relying on the original object.
157-
*
158-
* A reactive object also automatically unwraps refs contained in it, so you
159-
* don't need to use `.value` when accessing and mutating their value:
160-
*
161-
* ```js
162-
* const count = ref(0)
163-
* const obj = reactive({
164-
* count
165-
* })
166-
*
167-
* obj.count++
168-
* obj.count // -> 1
169-
* count.value // -> 1
170-
* ```
171-
*/
172-
export declare function reactive<T extends object>(
173-
target: T,
174-
): UnwrapNestedRefs<T>
175-
176-
/**
177-
* Creates a readonly copy of the original object. Note the returned copy is not
178-
* made reactive, but `readonly` can be called on an already reactive object.
179-
*/
180-
export declare function readonly<T extends object>(
181-
target: T,
182-
): DeepReadonly<UnwrapNestedRefs<T>>
183-
184-
export declare interface Ref<T = any> extends FunctionalRef<T, true> {
185-
value: T
186-
/**
187-
* Type differentiator only.
188-
* We need this to be in public d.ts but don't want it to show up in IDE
189-
* autocomplete, so we use a private Symbol instead.
190-
*/
191-
[RefSymbol]: true
192-
}
193-
194-
export declare function ref<T extends object>(
195-
value: T,
196-
): [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
197-
198-
export declare function ref<T>(value: T): Ref<UnwrapRef<T>>
199-
200-
export declare function ref<T = any>(): Ref<T | undefined>
201-
202-
export declare type ShallowReactive<T> = T & {
203-
[ShallowReactiveMarker]?: true
204-
}
205-
206-
/**
207-
* Return a shallowly-reactive copy of the original object, where only the root
208-
* level properties are reactive. It also does not auto-unwrap refs (even at the
209-
* root level).
210-
*/
211-
export declare function shallowReactive<T extends object>(
212-
target: T,
213-
): ShallowReactive<T>
16+
export function readonly<T extends object>(
17+
target: T,
18+
): DeepReadonlyFunctionalRef<UnwrapNestedRefs<T>>
21419

215-
export declare type ShallowRef<T = any> = Ref<T> & {
216-
[ShallowRefMarker]?: true
20+
export type ReadonlyRef<T> = Omit<T, 'set' | 'mutate'>
21+
export type DeepReadonlyFunctionalRef<T> =
22+
T extends Ref<any> ? ReadonlyRef<DeepReadonly<T>> : DeepReadonly<T>
21723
}
218-
219-
export declare function shallowRef<T extends object>(
220-
value: T,
221-
): T extends Ref ? T : ShallowRef<T>
222-
223-
export declare function shallowRef<T>(value: T): ShallowRef<T>
224-
225-
export declare function shallowRef<T = any>(): ShallowRef<T | undefined>
226-
227-
export declare type ShallowUnwrapRef<T> = {
228-
[K in keyof T]: T[K] extends Ref<infer V>
229-
? V
230-
: T[K] extends Ref<infer V> | undefined
231-
? unknown extends V
232-
? undefined
233-
: V | undefined
234-
: T[K]
235-
}
236-
237-
export declare type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
238-
239-
export declare function toRef<T extends object, K extends keyof T>(
240-
object: T,
241-
key: K,
242-
): ToRef<T[K]>
243-
244-
export declare function toRef<T extends object, K extends keyof T>(
245-
object: T,
246-
key: K,
247-
defaultValue: T[K],
248-
): ToRef<Exclude<T[K], undefined>>
249-
250-
export declare type ToRefs<T = any> = {
251-
[K in keyof T]: ToRef<T[K]>
252-
}
253-
254-
export declare function toRefs<T extends object>(object: T): ToRefs<T>
255-
256-
export declare function triggerRef(ref: Ref): void
257-
258-
export declare function unref<T>(ref: T | Ref<T>): T
259-
260-
export declare type UnwrapNestedRefs<T> = T extends Ref ? T : UnwrapRefSimple<T>
261-
262-
export declare type UnwrapRef<T> =
263-
T extends ShallowRef<infer V>
264-
? V
265-
: T extends Ref<infer V>
266-
? UnwrapRefSimple<V>
267-
: UnwrapRefSimple<T>
268-
269-
declare type UnwrapRefSimple<T> = T extends
270-
| Function
271-
| CollectionTypes
272-
| BaseTypes
273-
| Ref
274-
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
275-
| {
276-
[RawSymbol]?: true
277-
}
278-
? T
279-
: T extends Array<any>
280-
? {
281-
[K in keyof T]: UnwrapRefSimple<T[K]>
282-
}
283-
: T extends object & {
284-
[ShallowReactiveMarker]?: never
285-
}
286-
? {
287-
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
288-
}
289-
: T
290-
291-
export declare interface WritableComputedRef<T> extends Ref<T> {
292-
readonly effect: ReactiveEffect<T>
293-
}
294-
295-
export type MaybeRef<T = any> = T | Ref<T>
296-
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
297-
298-
export declare function toValue<T>(
299-
source: MaybeRefOrGetter<T> | ComputedRef<T>,
300-
): T

0 commit comments

Comments
 (0)