@@ -37,33 +37,33 @@ import type {
37
37
import { customInstance } from '../mutator/custom-instance' ;
38
38
import type { ErrorType } from '../mutator/custom-instance' ;
39
39
40
- type IsAny < T > = 0 extends 1 & T ? true : false ;
41
- type IsUnknown < T > =
42
- IsAny < T > extends true ? false : unknown extends T ? true : false ;
43
- type Primitive = string | number | boolean | bigint | symbol | undefined | null ;
44
- type isBuiltin = Primitive | Function | Date | Error | RegExp ;
45
- type NonReadonly < T > =
46
- T extends Exclude < isBuiltin , Error >
47
- ? T
48
- : T extends Map < infer Key , infer Value >
49
- ? Map < NonReadonly < Key > , NonReadonly < Value > >
50
- : T extends ReadonlyMap < infer Key , infer Value >
51
- ? Map < NonReadonly < Key > , NonReadonly < Value > >
52
- : T extends WeakMap < infer Key , infer Value >
53
- ? WeakMap < NonReadonly < Key > , NonReadonly < Value > >
54
- : T extends Set < infer Values >
55
- ? Set < NonReadonly < Values > >
56
- : T extends ReadonlySet < infer Values >
57
- ? Set < NonReadonly < Values > >
58
- : T extends WeakSet < infer Values >
59
- ? WeakSet < NonReadonly < Values > >
60
- : T extends Promise < infer Value >
61
- ? Promise < NonReadonly < Value > >
62
- : T extends { }
63
- ? { - readonly [ Key in keyof T ] : NonReadonly < T [ Key ] > }
64
- : IsUnknown < T > extends true
65
- ? unknown
66
- : T ;
40
+ // https://stackoverflow.com/questions/49579094/typescript-conditional-types-filter-out-readonly-properties-pick-only-requir/49579497#49579497
41
+ type IfEquals < X , Y , A = X , B = never > =
42
+ ( < T > ( ) => T extends X ? 1 : 2 ) extends < T > ( ) => T extends Y ? 1 : 2 ? A : B ;
43
+
44
+ type WritableKeys < T > = {
45
+ [ P in keyof T ] -?: IfEquals <
46
+ { [ Q in P ] : T [ P ] } ,
47
+ { - readonly [ Q in P ] : T [ P ] } ,
48
+ P
49
+ > ;
50
+ } [ keyof T ] ;
51
+
52
+ type UnionToIntersection < U > = ( U extends any ? ( k : U ) => void : never ) extends (
53
+ k : infer I ,
54
+ ) => void
55
+ ? I
56
+ : never ;
57
+ type DistributeReadOnlyOverUnions < T > = T extends any ? NonReadonly < T > : never ;
58
+
59
+ type Writable < T > = Pick < T , WritableKeys < T > > ;
60
+ type NonReadonly < T > = [ T ] extends [ UnionToIntersection < T > ]
61
+ ? {
62
+ [ P in keyof Writable < T > ] : T [ P ] extends object
63
+ ? NonReadonly < NonNullable < T [ P ] > >
64
+ : T [ P ] ;
65
+ }
66
+ : DistributeReadOnlyOverUnions < T > ;
67
67
68
68
type AwaitedInput < T > = PromiseLike < T > | T ;
69
69
0 commit comments