fix: use answer from stackoverflow

This commit is contained in:
KazariEX 2025-02-24 14:56:04 +08:00
parent c2f762249c
commit 425ece0d5d
2 changed files with 15 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import {
type IfAny,
type IfEquals,
hasChanged,
isArray,
isFunction,
@ -486,14 +487,19 @@ function propertyToRef(
export interface RefUnwrapBailTypes {}
export type ShallowUnwrapRef<T> = {
readonly [K in keyof Pick<T, ReadonlyKeys<T>>]: DistributeRef<T[K]>
[K in keyof Pick<T, MutableKeys<T>>]: DistributeRef<T[K]>
} & {
[K in keyof Omit<T, ReadonlyKeys<T>>]: DistributeRef<T[K]>
readonly [K in keyof Omit<T, MutableKeys<T>>]: DistributeRef<T[K]>
}
type ReadonlyKeys<O> = {
[K in keyof O]: O[K] extends Readonly<any> ? K : never
}[keyof O]
type MutableKeys<T> = {
[P in keyof T]-?: IfEquals<
{ [Q in P]: T[P] },
{ -readonly [Q in P]: T[P] },
P,
never
>
}[keyof T]
type DistributeRef<T> = T extends Ref<infer V, unknown> ? V : T

View File

@ -13,6 +13,10 @@ export type LooseRequired<T> = { [P in keyof (T & Required<T>)]: T[P] }
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N
// https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript/52473108#52473108
export type IfEquals<A, B, Y, N> =
(<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? Y : N
export type IsKeyValues<T, K = string> = IfAny<
T,
false,