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