|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +export type PayloadAction< |
| 3 | + Payload = void, |
| 4 | + Type extends string = string, |
| 5 | + Meta = never, |
| 6 | + Error = never |
| 7 | +> = { |
| 8 | + payload: Payload; |
| 9 | + type: Type; |
| 10 | +} & ([Meta] extends [never] ? unknown : { meta: Meta }) |
| 11 | + & ([Error] extends [never] ? unknown : { error: Error }); |
| 12 | + |
| 13 | +type ActionLike = { |
| 14 | + type: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export type PrepareAction<Payload> = |
| 18 | + | ((...args: any[]) => { payload: Payload }) |
| 19 | + | ((...args: any[]) => { payload: Payload; meta: any }) |
| 20 | + | ((...args: any[]) => { payload: Payload; error: any }) |
| 21 | + | ((...args: any[]) => { payload: Payload; meta: any; error: any }); |
| 22 | + |
| 23 | +type IsAny<T, True, False = never> = true | false extends ( |
| 24 | + T extends never ? true : false |
| 25 | +) |
| 26 | + ? True |
| 27 | + : False; |
| 28 | + |
| 29 | +type IsUnknown<T, True, False = never> = unknown extends T |
| 30 | + ? IsAny<T, False, True> |
| 31 | + : False; |
| 32 | + |
| 33 | +type IfMaybeUndefined<Payload, True, False> = [undefined] extends [Payload] |
| 34 | + ? True |
| 35 | + : False; |
| 36 | + |
| 37 | +type IfVoid<Payload, True, False> = [void] extends [Payload] ? True : False; |
| 38 | + |
| 39 | +type IsEmptyObject<T, True, False = never> = T extends unknown |
| 40 | + ? keyof T extends never |
| 41 | + ? IsUnknown<T, False, IfMaybeUndefined<T, False, IfVoid<T, False, True>>> |
| 42 | + : False |
| 43 | + : never; |
| 44 | + |
| 45 | +type AtLeastTs35<True, False> = [True, False][IsUnknown< |
| 46 | + ReturnType<(<T>() => T)>, |
| 47 | + 0, |
| 48 | + 1 |
| 49 | +>]; |
| 50 | + |
| 51 | +type IsUnknownOrNonInferrable<T, True, False> = AtLeastTs35< |
| 52 | + IsUnknown<T, True, False>, |
| 53 | + IsEmptyObject<T, True, IsUnknown<T, True, False>> |
| 54 | +>; |
| 55 | + |
| 56 | +type BaseActionCreator< |
| 57 | + Payload, |
| 58 | + Type extends string, |
| 59 | + Meta = never, |
| 60 | + Error = never |
| 61 | +> = { |
| 62 | + type: Type; |
| 63 | + toString(): Type; |
| 64 | + match(action: unknown): action is PayloadAction<Payload, Type, Meta, Error>; |
| 65 | +}; |
| 66 | + |
| 67 | +export type ActionCreatorWithPayload< |
| 68 | + Payload, |
| 69 | + Type extends string = string |
| 70 | +> = BaseActionCreator<Payload, Type> & { |
| 71 | + (payload: Payload): PayloadAction<Payload, Type>; |
| 72 | +}; |
| 73 | + |
| 74 | +export type ActionCreatorWithOptionalPayload< |
| 75 | + Payload, |
| 76 | + Type extends string = string |
| 77 | +> = BaseActionCreator<Payload, Type> & { |
| 78 | + (payload?: Payload): PayloadAction<Payload, Type>; |
| 79 | +}; |
| 80 | + |
| 81 | +export type ActionCreatorWithoutPayload< |
| 82 | + Type extends string = string |
| 83 | +> = BaseActionCreator<undefined, Type> & { |
| 84 | + (noArgument: void): PayloadAction<undefined, Type>; |
| 85 | +}; |
| 86 | + |
| 87 | +export type ActionCreatorWithNonInferrablePayload< |
| 88 | + Type extends string = string |
| 89 | +> = BaseActionCreator<unknown, Type> & { |
| 90 | + <Payload>(payload: Payload): PayloadAction<Payload, Type>; |
| 91 | +}; |
| 92 | + |
| 93 | +export type ActionCreatorWithPreparedPayload< |
| 94 | + Args extends unknown[], |
| 95 | + Payload, |
| 96 | + Type extends string = string, |
| 97 | + Error = never, |
| 98 | + Meta = never |
| 99 | +> = BaseActionCreator<Payload, Type, Meta, Error> & { |
| 100 | + (...args: Args): PayloadAction<Payload, Type, Meta, Error>; |
| 101 | +}; |
| 102 | + |
| 103 | +type PreparedPayloadActionCreator< |
| 104 | + Prepare extends PrepareAction<any>, |
| 105 | + Type extends string |
| 106 | +> = ActionCreatorWithPreparedPayload< |
| 107 | + Parameters<Prepare>, |
| 108 | + ReturnType<Prepare>['payload'], |
| 109 | + Type, |
| 110 | + ReturnType<Prepare> extends { error: infer Error } ? Error : never, |
| 111 | + ReturnType<Prepare> extends { meta: infer Meta } ? Meta : never |
| 112 | +>; |
| 113 | + |
| 114 | +type _ActionCreatorWithPreparedPayload< |
| 115 | + Prepare extends PrepareAction<any> | void, |
| 116 | + Type extends string = string |
| 117 | +> = Prepare extends PrepareAction<any> |
| 118 | + ? PreparedPayloadActionCreator<Prepare, Type> |
| 119 | + : void; |
| 120 | + |
| 121 | +type IfPrepareActionMethodProvided<Prepare, True, False> = Prepare extends ( |
| 122 | + ...args: any[] |
| 123 | +) => any |
| 124 | + ? True |
| 125 | + : False; |
| 126 | + |
| 127 | +export type PayloadActionCreator< |
| 128 | + Payload = void, |
| 129 | + Type extends string = string, |
| 130 | + Prepare extends PrepareAction<Payload> | void = void |
| 131 | +> = IfPrepareActionMethodProvided< |
| 132 | + Prepare, |
| 133 | + _ActionCreatorWithPreparedPayload<Prepare, Type>, |
| 134 | + IsAny< |
| 135 | + Payload, |
| 136 | + // `any` must stay explicit here to preserve RTK-compatible payload behavior. |
| 137 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 138 | + ActionCreatorWithPayload<any, Type>, |
| 139 | + IsUnknownOrNonInferrable< |
| 140 | + Payload, |
| 141 | + ActionCreatorWithNonInferrablePayload<Type>, |
| 142 | + IfVoid< |
| 143 | + Payload, |
| 144 | + ActionCreatorWithoutPayload<Type>, |
| 145 | + IfMaybeUndefined< |
| 146 | + Payload, |
| 147 | + ActionCreatorWithOptionalPayload<Payload, Type>, |
| 148 | + ActionCreatorWithPayload<Payload, Type> |
| 149 | + > |
| 150 | + > |
| 151 | + > |
| 152 | + > |
| 153 | +>; |
| 154 | + |
| 155 | +export function createAction<Payload = void, Type extends string = string>( |
| 156 | + type: Type |
| 157 | +): PayloadActionCreator<Payload, Type>; |
| 158 | +export function createAction< |
| 159 | + Prepare extends PrepareAction<any>, |
| 160 | + Type extends string = string |
| 161 | +>( |
| 162 | + type: Type, |
| 163 | + prepareAction: Prepare |
| 164 | +): PayloadActionCreator<ReturnType<Prepare>['payload'], Type, Prepare>; |
| 165 | +export function createAction(type: string, prepareAction?: PrepareAction<any>) { |
| 166 | + const actionCreator = ((...args: unknown[]) => { |
| 167 | + if (prepareAction) { |
| 168 | + return { |
| 169 | + ...prepareAction(...args), |
| 170 | + type, |
| 171 | + }; |
| 172 | + } |
| 173 | + |
| 174 | + return { |
| 175 | + payload: args[0], |
| 176 | + type, |
| 177 | + }; |
| 178 | + }) as PayloadActionCreator<unknown, string>; |
| 179 | + |
| 180 | + actionCreator.type = type; |
| 181 | + actionCreator.toString = () => type; |
| 182 | + actionCreator.match = ( |
| 183 | + action: unknown |
| 184 | + ): action is PayloadAction<unknown, string> => |
| 185 | + typeof action === 'object' |
| 186 | + && action !== null |
| 187 | + && 'type' in action |
| 188 | + && (action as ActionLike).type === type; |
| 189 | + |
| 190 | + return actionCreator; |
| 191 | +} |
0 commit comments