Skip to content

Commit

Permalink
feat(types): allow computed getter and setter types to be unrelated (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue authored Aug 5, 2024
1 parent 5ffd1a8 commit a01675e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 20 additions & 0 deletions packages/dts-test/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ describe('allow getter and setter types to be unrelated', <T>() => {
e.value = d
})

// computed
describe('allow computed getter and setter types to be unrelated', () => {
const obj = ref({
name: 'foo',
})

const c = computed({
get() {
return JSON.stringify(obj.value)
},
set(val: typeof obj.value) {
obj.value = val
},
})

c.value = { name: 'bar' } // object

expectType<string>(c.value)
})

// shallowRef
type Status = 'initial' | 'ready' | 'invalidating'
const shallowStatus = shallowRef<Status>('initial')
Expand Down
12 changes: 6 additions & 6 deletions packages/reactivity/src/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface ComputedRef<T = any> extends WritableComputedRef<T> {
[ComputedRefSymbol]: true
}

export interface WritableComputedRef<T> extends Ref<T> {
export interface WritableComputedRef<T, S = T> extends Ref<T, S> {
/**
* @deprecated computed no longer uses effect
*/
Expand All @@ -30,9 +30,9 @@ export interface WritableComputedRef<T> extends Ref<T> {
export type ComputedGetter<T> = (oldValue?: T) => T
export type ComputedSetter<T> = (newValue: T) => void

export interface WritableComputedOptions<T> {
export interface WritableComputedOptions<T, S = T> {
get: ComputedGetter<T>
set: ComputedSetter<T>
set: ComputedSetter<S>
}

/**
Expand Down Expand Up @@ -175,10 +175,10 @@ export function computed<T>(
getter: ComputedGetter<T>,
debugOptions?: DebuggerOptions,
): ComputedRef<T>
export function computed<T>(
options: WritableComputedOptions<T>,
export function computed<T, S = T>(
options: WritableComputedOptions<T, S>,
debugOptions?: DebuggerOptions,
): WritableComputedRef<T>
): WritableComputedRef<T, S>
export function computed<T>(
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>,
debugOptions?: DebuggerOptions,
Expand Down

0 comments on commit a01675e

Please sign in to comment.