(value: T)
| 1157 | : { -readonly [P in keyof T]: DeepWritable<T[P]> } |
| 1158 | |
| 1159 | export function deepClone<T>(value: T): DeepWritable<T> { |
| 1160 | if (Array.isArray(value)) { |
| 1161 | return value.map((v) => deepClone(v)) as DeepWritable<T> |
| 1162 | } |
| 1163 | if (isObject(value)) { |
| 1164 | const cloned: Record<string, any> = {} |
| 1165 | for (const key in value) { |
| 1166 | cloned[key] = deepClone(value[key]) |
| 1167 | } |
| 1168 | return cloned as DeepWritable<T> |
| 1169 | } |
| 1170 | if (typeof value === class="st">'function') { |
| 1171 | return value as DeepWritable<T> |
| 1172 | } |
| 1173 | if (value instanceof RegExp) { |
| 1174 | return new RegExp(value) as DeepWritable<T> |
| 1175 | } |
| 1176 | if (typeof value === class="st">'object' && value != null) { |
| 1177 | throw new Error(class="st">'Cannot deep clone non-plain object') |
| 1178 | } |
| 1179 | return value as DeepWritable<T> |
| 1180 | } |
| 1181 | |
| 1182 | type MaybeFallback<D, V> = undefined extends V ? Exclude<V, undefined> | D : V |
| 1183 |
no test coverage detected