rambdax
Version:
Extended version of Rambda - a lightweight, faster alternative to Ramda
51 lines (45 loc) • 1.12 kB
text/typescript
import {_Pick} from './Pick'
import {Depth} from './_Internal'
import {Key} from '../Any/Key'
import {_PatchFlat} from './Patch'
import {BuiltInObject} from '../Misc/BuiltInObject'
/**
@hidden
*/
export type WritableFlat<O> = {
-readonly [K in keyof O]: O[K]
} & {}
/**
@hidden
*/
export type WritableDeep<O> = {
-readonly [K in keyof O]: O[K] extends BuiltInObject
? O[K]
: WritableDeep<O[K]>
}
/**
@hidden
*/
export type WritablePart<O extends object, depth extends Depth> = {
'flat': WritableFlat<O>,
'deep': WritableDeep<O>,
}[depth]
/**
@hidden
*/
export type _Writable<O extends object, K extends Key, depth extends Depth> =
_PatchFlat<WritablePart<_Pick<O, K>, depth>, O, 2>
/**
Make some fields of `O` writable (deeply or not)
@param O to make writable
@param K (?=`Key`) to choose fields
@param depth (?=`'flat'`) to do it deeply
@returns [[Object]]
@example
```ts
```
*/
export type Writable<O extends object, K extends Key = Key, depth extends Depth = 'flat'> =
O extends unknown
? _Writable<O, K, depth>
: never