feat(types/reactivity): use DeepReadonly type for readonly return type (#1462)

close #1452
This commit is contained in:
Pick
2020-07-15 21:27:21 +08:00
committed by GitHub
parent 246ec5c594
commit b772bba558
3 changed files with 33 additions and 2 deletions

View File

@@ -72,9 +72,31 @@ export function shallowReactive<T extends object>(target: T): T {
)
}
type Primitive = string | number | boolean | bigint | symbol | undefined | null
type Builtin = Primitive | Function | Date | Error | RegExp
type DeepReadonly<T> = T extends Builtin
? T
: T extends Map<infer K, infer V>
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
: T extends ReadonlyMap<infer K, infer V>
? ReadonlyMap<DeepReadonly<K>, DeepReadonly<V>>
: T extends WeakMap<infer K, infer V>
? WeakMap<DeepReadonly<K>, DeepReadonly<V>>
: T extends Set<infer U>
? ReadonlySet<DeepReadonly<U>>
: T extends ReadonlySet<infer U>
? ReadonlySet<DeepReadonly<U>>
: T extends WeakSet<infer U>
? WeakSet<DeepReadonly<U>>
: T extends Promise<infer U>
? Promise<DeepReadonly<U>>
: T extends {}
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: Readonly<T>
export function readonly<T extends object>(
target: T
): Readonly<UnwrapNestedRefs<T>> {
): DeepReadonly<UnwrapNestedRefs<T>> {
return createReactiveObject(
target,
true,