Skip to content

Commit

Permalink
fix(type): fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Sep 7, 2024
1 parent 56471b6 commit a9fde93
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
29 changes: 15 additions & 14 deletions src/apply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Operation, DraftType } from './interface';
import type { Draft, Options, Patches, ApplyMutableOptions } from './interface';
import type {
Draft,
Patches,
ApplyMutableOptions,
ApplyOptions,
ApplyResult,
} from './interface';
import { deepClone, get, getType, isDraft, unescapePath } from './utils';
import { create } from './create';

Expand All @@ -24,16 +30,11 @@ import { create } from './create';
* expect(state).toEqual(apply(baseState, patches));
* ```
*/
export function apply<T extends object, F extends boolean = false>(
state: T,
patches: Patches,
applyOptions?:
| Pick<
Options<boolean, F>,
Exclude<keyof Options<boolean, F>, 'enablePatches'>
>
| ApplyMutableOptions
) {
export function apply<
T extends object,
F extends boolean = false,
A extends ApplyOptions<F> = ApplyOptions<F>
>(state: T, patches: Patches, applyOptions?: A): ApplyResult<T, F, A> {
let i: number;
for (i = patches.length - 1; i >= 0; i -= 1) {
const { value, op, path } = patches[i];
Expand Down Expand Up @@ -127,17 +128,17 @@ export function apply<T extends object, F extends boolean = false>(
};
if ((applyOptions as ApplyMutableOptions)?.mutable) {
mutate(state);
return;
return undefined as ApplyResult<T, F, A>;
}
if (isDraft(state)) {
if (applyOptions !== undefined) {
throw new Error(`Cannot apply patches with options to a draft.`);
}
mutate(state as Draft<T>);
return state;
return state as ApplyResult<T, F, A>;
}
return create<T, F>(state, mutate, {
...applyOptions,
enablePatches: false,
});
}) as any;
}
13 changes: 13 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,16 @@ export type Draft<T> = T extends Primitive | AtomicObject
: T extends object
? DraftedObject<T>
: T;

export type ApplyOptions<F extends boolean> =
| Pick<
Options<boolean, F>,
Exclude<keyof Options<boolean, F>, 'enablePatches'>
>
| ApplyMutableOptions;

export type ApplyResult<
T extends object,
F extends boolean = false,
A extends ApplyOptions<F> = ApplyOptions<F>
> = A extends { mutable: true } ? void : T;
15 changes: 5 additions & 10 deletions test/apply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1305,16 +1305,6 @@ test('merge multiple patches', () => {
enablePatches: true,
}
);
console.log(
JSON.stringify(
{
patches,
inversePatches,
},
null,
2
)
);
const [state1, patches1, inversePatches1] = create(
state,
(draft) => {
Expand Down Expand Up @@ -1390,8 +1380,13 @@ test('base - mutate', () => {
},
],
});
const nextState = apply(baseState, patches);
expect(nextState).toEqual({ a: { c: 2 } });
expect(baseState).toEqual({ a: { c: 1 } });

const result = apply(baseState, patches, {
mutable: true,
});
expect(baseState).toEqual({ a: { c: 2 } });
expect(result).toBeUndefined();
});

0 comments on commit a9fde93

Please sign in to comment.