Skip to content

types(computed): allow computed getter and setter types to be unrelated #11472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -183,6 +183,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