Skip to content

Commit dbaca0a

Browse files
committed
fix(type): allow specify ref type to ref()
1 parent ca4ec33 commit dbaca0a

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/component/component.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue, { VueConstructor, VNode, ComponentOptions as Vue2ComponentOptions } from 'vue';
22
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps';
33
import { UnwrapRef } from '../reactivity';
4+
import { HasDefined } from '../types/basic';
45

56
export type Data = { [key: string]: unknown };
67

@@ -65,14 +66,6 @@ interface ComponentOptions<
6566
setup?: SetupFunction<Props, RawBindings>;
6667
}
6768

68-
// Conditional returns can enforce identical types.
69-
// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
70-
// prettier-ignore
71-
type Equal<Left, Right> =
72-
(<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? true : false;
73-
74-
type HasDefined<T> = Equal<T, unknown> extends true ? false : true;
75-
7669
// object format with object props declaration
7770
// see `ExtractPropTypes` in ./componentProps.ts
7871
export function createComponent<Props, RawBindings = Data, PropsOptions = ComponentPropsOptions>(

src/reactivity/ref.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Data } from '../component';
22
import { RefKey } from '../symbols';
33
import { proxy, isPlainObject } from '../utils';
4+
import { HasDefined } from '../types/basic';
45
import { reactive } from './reactive';
56

67
type BailTypes = Function | Map<any, any> | Set<any> | WeakMap<any, any> | WeakSet<any>;
@@ -106,12 +107,14 @@ type RefValue<T> = T extends Ref<infer V> ? V : UnwrapRef<T>;
106107
// without init value, explicit typed: a = ref<{ a: number }>()
107108
// typeof a will be Ref<{ a: number } | undefined>
108109
export function ref<T = undefined>(): Ref<T | undefined>;
109-
// with init value: a = ref({ a: ref(0) })
110-
// typeof a will be Ref<{ a: number }>
111-
export function ref<T, R = RefValue<T>>(raw: T): Ref<R>;
112110
// with null as init value: a = ref<{ a: number }>(null);
113111
// typeof a will be Ref<{ a: number } | null>
114-
export function ref<T, R = RefValue<T>>(raw: T | null): Ref<R | null>;
112+
export function ref<T = null>(raw: null): Ref<T | null>;
113+
// with init value: a = ref({ a: ref(0) })
114+
// typeof a will be Ref<{ a: number }>
115+
export function ref<S, T = unknown, R = HasDefined<S> extends true ? S : RefValue<T>>(
116+
raw: T
117+
): Ref<R>;
115118
// implementation
116119
export function ref(raw?: any): any {
117120
// if (isRef(raw)) {

src/types/basic.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
export type AnyObject = Record<string | number | symbol, any>;
2+
3+
// Conditional returns can enforce identical types.
4+
// See here: https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
5+
// prettier-ignore
6+
type Equal<Left, Right> =
7+
(<U>() => U extends Left ? 1 : 0) extends (<U>() => U extends Right ? 1 : 0) ? true : false;
8+
9+
export type HasDefined<T> = Equal<T, unknown> extends true ? false : true;

0 commit comments

Comments
 (0)