Subscribe on changes!

Misstyped reference types

avatar
Sep 10th 2020

Version

3.0.0-rc.10

Reproduction link

No need

Steps to reproduce

The vue website cites the ref(value) always return a Ref type.

This is not the same as the real exported types.

Also, this leads to the following 2-line codes to the following result:

1st: Type 'T' is not assignable to type 'UnwrapRef' 2nd: OK

// First
const result = ref<T>(original);
result.value = original;

// Second
const result2 = ref<T>();
result2.value = original;

What is expected?

Type as seen on the documentation.

interface Ref<T> {
  value: T
}

function ref<T>(value: T): Ref<T>

What is actually happening?

Real exported types

export declare function ref<T extends object>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>;
export declare function ref<T>(value: T): Ref<UnwrapRef<T>>;
export declare function ref<T = any>(): Ref<T | undefined>;
avatar
Sep 10th 2020

This is expected, when calling ref on a ref, it doesn't wrap an extra ref layer on it so you can write foo.value instead of foo.value.value

avatar
Sep 10th 2020

@posva The thing is this happens if u call it on a normal value, not a ref.

ref(10) for example