Skip to content

Support reactive object with 'value' property #167

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 4 additions & 6 deletions src/reactivity/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ export interface Ref<T> {
// Recursively unwraps nested value bindings.
// Unfortunately TS cannot do recursive types, but this should be enough for
// practical use cases...
export type UnwrapRef<T> = T extends Ref<infer V>
? UnwrapRef2<V>
: T extends BailTypes
? T // bail out on types that shouldn't be unwrapped
: T extends object ? { [K in keyof T]: UnwrapRef2<T[K]> } : T
export type UnwrapRef<T> = T extends BailTypes
? T // bail out on types that shouldn't be unwrapped
: T extends object ? { [K in keyof T]: UnwrapRef2<T[K]> } : T

// prettier-ignore
type UnwrapRef2<T> = T extends Ref<infer V>
Expand Down Expand Up @@ -134,7 +132,7 @@ export function isRef<T>(value: any): value is Ref<T> {

// prettier-ignore
type Refs<Data> = {
[K in keyof Data]: Data[K] extends Ref<infer V>
[K in keyof Data]: Data[K] extends Ref<infer V>
? Ref<V>
: Ref<Data[K]>
}
Expand Down
8 changes: 8 additions & 0 deletions test/types/reactive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { reactive, UnwrapRef } from '../../src/reactivity';

describe('reactive', () => {
it('should accept objects with a "value" property', () => {
const x: UnwrapRef<{ value: string }> = reactive({ value: 'foo' });
expect(x.value).toEqual('foo');
});
});